R
Regex Master
TutorialsToolsFAQAboutContact
  1. Home
  2. Tutorials
  3. Text Processing
  4. Remove Whitespace and Newlines from String Using Regex
March 20, 2024Regex Master Team9 min read

Remove Whitespace and Newlines from String Using Regex

Text Processingwhitespace removalregex patternsstring manipulationtext cleaning

Learn how to remove spaces, tabs, and newlines from strings using regular expressions with practical examples.

Remove Whitespace and Newlines from String Using Regex

Removing whitespace is a common text processing task for data cleaning, user input sanitization, and text formatting. Regular expressions provide powerful and flexible ways to handle various types of whitespace characters. In this comprehensive guide, we'll explore patterns for removing different types of whitespace and practical applications.

Understanding Whitespace Characters

Different whitespace characters exist in text:

CharacterNameHexRegex Escape
SpaceSpace0x20\s or
TabHorizontal tab0x09\t
NewlineLine feed0x0A\n
Carriage ReturnCarriage return0x0D\r
Non-breaking spaceNBSP0xA0\u00A0

Basic Whitespace Removal

Remove All Whitespace

\s+

Example: "Hello World" → "HelloWorld"

JavaScript:

const text = "Hello World";
const cleaned = text.replace(/\s+/g, '');
console.log(cleaned); // "HelloWorld"

Remove All Whitespace (Unicode)

[\s\u00A0]+

Includes non-breaking spaces and other Unicode whitespace.

Specific Whitespace Types

Remove Spaces Only

[ ]+

Example: "Hello World" → "HelloWorld"

const cleaned = text.replace(/[ ]+/g, '');

Remove Tabs

\t+

Example: "Hello\tWorld" → "HelloWorld"

const cleaned = text.replace(/\t+/g, '');

Remove Newlines

\n+

Example: "Hello\nWorld" → "HelloWorld"

const cleaned = text.replace(/\n+/g, '');

Remove Carriage Returns

\r+

Example: "Hello\rWorld" → "HelloWorld"

const cleaned = text.replace(/\r+/g, '');

Advanced Whitespace Handling

Remove All Line Breaks (CRLF, LF, CR)

\r\n|\r|\n

Example: "Hello\r\nWorld\nTest" → "HelloWorldTest"

const cleaned = text.replace(/\r\n|\r|\n/g, '');

Replace Multiple Spaces with Single Space

\s+

Example: "Hello World" → "Hello World"

const cleaned = text.replace(/\s+/g, ' ');

Remove Leading Whitespace

^\s+

Example: " Hello World" → "Hello World"

const cleaned = text.replace(/^\s+/, '');

Remove Trailing Whitespace

\s+$

Example: "Hello World " → "Hello World"

const cleaned = text.replace(/\s+$/, '');

Trim (Remove Both Leading and Trailing)

^\s+|\s+$

Example: " Hello World " → "Hello World"

const cleaned = text.replace(/^\s+|\s+$/g, '');

Alternative: Use built-in trim() method

const cleaned = text.trim();

Practical Use Cases

Clean User Input

function cleanUserInput(input) {
  // Remove leading/trailing whitespace
  let cleaned = input.trim();
  
  // Replace multiple spaces with single space
  cleaned = cleaned.replace(/\s+/g, ' ');
  
  // Remove non-breaking spaces
  cleaned = cleaned.replace(/[\u00A0\u2000-\u200B\u202F\u205F\u3000]/g, ' ');
  
  return cleaned;
}

const userInput = "  Hello    World  ";
console.log(cleanUserInput(userInput)); // "Hello World"

Normalize Line Endings

function normalizeLineEndings(text) {
  // Convert CRLF to LF
  text = text.replace(/\r\n/g, '\n');
  // Convert CR to LF
  text = text.replace(/\r/g, '\n');
  return text;
}

const mixed = "Line 1\r\nLine 2\rLine 3\n";
console.log(normalizeLineEndings(mixed));
// "Line 1\nLine 2\nLine 3\n"

Clean CSV Data

function cleanCSV(csvText) {
  // Remove whitespace around fields
  let cleaned = csvText.replace(/\s*,\s*/g, ',');
  
  // Remove whitespace around quotes
  cleaned = cleaned.replace(/\s*"([^"]*)"\s*/g, '"$1"');
  
  return cleaned;
}

const csv = "  Name ,  Age ,  City  ";
console.log(cleanCSV(csv));
// "Name,Age,City"

Remove Whitespace from Phone Numbers

function cleanPhoneNumber(phone) {
  // Remove all whitespace
  return phone.replace(/\s/g, '');
}

const phone = "(555) 123 4567";
console.log(cleanPhoneNumber(phone)); // "(555)1234567"

Prepare Text for URLs

function slugify(text) {
  // Replace spaces with hyphens
  let slug = text.replace(/\s+/g, '-');
  
  // Remove special characters
  slug = slug.replace(/[^\w-]/g, '');
  
  // Remove multiple hyphens
  slug = slug.replace(/-+/g, '-');
  
  // Remove leading/trailing hyphens
  slug = slug.replace(/^-+|-+$/g, '');
  
  return slug.toLowerCase();
}

const title = "Hello World  Example";
console.log(slugify(title)); // "hello-world-example"

Code Examples by Language

JavaScript/TypeScript

// Remove all whitespace
const noWhitespace = text.replace(/\s/g, '');

// Replace multiple spaces with single
const singleSpace = text.replace(/\s+/g, ' ');

// Trim and normalize
const cleaned = text.trim().replace(/\s+/g, ' ');

// Remove only spaces (keep tabs/newlines)
const noSpaces = text.replace(/ /g, '');

Python

import re

# Remove all whitespace
no_whitespace = re.sub(r'\s+', '', text)

# Replace multiple spaces with single
single_space = re.sub(r'\s+', ' ', text)

# Trim and normalize
cleaned = text.strip()
cleaned = re.sub(r'\s+', ' ', cleaned)

PHP

// Remove all whitespace
$noWhitespace = preg_replace('/\s+/', '', $text);

// Replace multiple spaces with single
$singleSpace = preg_replace('/\s+/', ' ', $text);

// Trim and normalize
$cleaned = trim($text);
$cleaned = preg_replace('/\s+/', ' ', $cleaned);

Java

import java.util.regex.Pattern;
import java.util.regex.Matcher;

// Remove all whitespace
String noWhitespace = text.replaceAll("\\s+", "");

// Replace multiple spaces with single
String singleSpace = text.replaceAll("\\s+", " ");

// Trim and normalize
String cleaned = text.trim().replaceAll("\\s+", " ");

Special Cases

Remove Zero-Width Spaces

[\u200B-\u200D\uFEFF]+

Zero-width spaces are invisible but can cause issues:

const text = "Hello\u200BWorld";
const cleaned = text.replace(/[\u200B-\u200D\uFEFF]+/g, '');
console.log(cleaned); // "HelloWorld"

Remove Emojis and Symbols (Whitespace-like)

[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F1E0}-\u{1F1FF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}]

Keep Only Alphanumeric

[^a-zA-Z0-9]+

Example: "Hello, World! 123" → "HelloWorld123"

const alphanumeric = text.replace(/[^a-zA-Z0-9]+/g, '');

Best Practices

1. Test Before Replacing

function safeReplace(text, pattern, replacement) {
  if (pattern.test(text)) {
    return text.replace(pattern, replacement);
  }
  return text;
}

2. Use String Methods When Possible

// GOOD: Use built-in trim()
const trimmed = text.trim();

// AVOID: Regex for simple trimming
const trimmed = text.replace(/^\s+|\s+$/g, '');

3. Preserve Intent When Cleaning

// GOOD: Replace multiple spaces with single
const cleaned = text.replace(/\s+/g, ' ');

// AVOID: Remove all spaces
const cleaned = text.replace(/\s+/g, '');

4. Handle Unicode Correctly

// GOOD: Include Unicode whitespace
const cleaned = text.replace(/[\s\u00A0\u2000-\u200B]+/g, '');

// AVOID: Only ASCII whitespace
const cleaned = text.replace(/\s+/g, '');

Common Pitfalls

Pitfall 1: Not Using Global Flag

// WRONG: Only removes first occurrence
const cleaned = text.replace(/\s/, '');

// RIGHT: Removes all occurrences
const cleaned = text.replace(/\s/g, '');

Pitfall 2: Removing Too Much

// WRONG: Removes all whitespace, loses word boundaries
const cleaned = text.replace(/\s+/g, '');

// RIGHT: Replace with single space
const cleaned = text.replace(/\s+/g, ' ');

Pitfall 3: Forgetting Unicode Whitespace

// WRONG: Misses non-breaking spaces
const cleaned = text.replace(/\s+/g, ' ');

// RIGHT: Includes Unicode whitespace
const cleaned = text.replace(/[\s\u00A0\u2000-\u200B]+/g, ' ');

Pitfall 4: Not Considering Performance

// GOOD: Chain operations
let cleaned = text.trim();
cleaned = cleaned.replace(/\s+/g, ' ');

// AVOID: Complex single pattern
const cleaned = text.replace(/^\s+|\s+$|\s+/g, ' ');

Testing Your Whitespace Removal

Use our interactive String Replace tool with these test cases:

Input: " Hello World \r\n\tTest "

Expected Results:

  • Remove all: "HelloWorldTest"
  • Replace with single space: "Hello World Test"
  • Trim only: "Hello World \r\n\tTest"
  • Remove newlines: " Hello World \tTest "

Edge Cases:

  • Unicode whitespace: "Hello\u00A0World"
  • Zero-width spaces: "Hello\u200BWorld"
  • Mixed line endings: "Line 1\r\nLine 2\rLine 3\n"

Performance Considerations

For Large Texts

// Option 1: Use built-in methods when possible
const cleaned = text.trim();

// Option 2: Use global flag for multiple replacements
const cleaned = text.replace(/\s+/g, '');

// Option 3: Split and join (sometimes faster)
const cleaned = text.split(/\s+/).filter(Boolean).join(' ');

For Processing Multiple Strings

// Pre-compile regex for reuse
const whitespaceRegex = /\s+/g;

function cleanAll(texts) {
  return texts.map(text => text.replace(whitespaceRegex, ' '));
}

Conclusion

Whitespace removal with regex is straightforward but requires attention to detail:

  1. Use \s for general whitespace matching
  2. Use global flag to remove all occurrences
  3. Consider Unicode for international text
  4. Test edge cases like zero-width spaces
  5. Use built-in methods when appropriate (trim())

The pattern \s+ with global flag provides basic whitespace removal, while [\s\u00A0\u2000-\u200B]+ handles Unicode whitespace.

Remember to preserve the intent of your text - sometimes replacing with single space is better than removing completely.

Experiment with different patterns using our String Replace tool to find the perfect fit for your text processing needs!


About the Author

The Regex Master Team consists of experienced developers and technical writers dedicated to simplifying regular expressions for everyone. We ensure all patterns are rigorously tested and verified to provide accurate, production-ready solutions.

Try It: Regex Tester

Use our interactive regex tester to experiment with the patterns you learned in this article. Test your regular expressions in real-time and see immediate results.

Loading tester...
R
Regex Master

Your comprehensive guide to mastering regular expressions through tutorials and tools.

Company

  • About Us
  • Contact
  • FAQ

Resources

  • All Articles
  • Popular Tools
  • Sitemap

Legal

  • Privacy Policy
  • Terms of Service
  • Cookie Policy
  • Disclaimer

© 2026 Regex Master. All rights reserved.