Text Tools
Whitespace Cleaner - Remove & Fix Whitespace in Text
Clean up whitespace in text: trim lines, collapse spaces, remove or collapse blank lines, and convert tabs to spaces.
Options
What does the whitespace cleaner do?
This tool gives you fine-grained control over whitespace in text. You can trim leading and trailing spaces from every line, collapse multiple consecutive spaces to one, remove or collapse blank lines, and convert tab characters to spaces.
Common use cases
- Cleaning up text copied from PDFs or web pages that includes irregular spacing.
- Normalizing whitespace before inserting text into a database or document.
- Removing extra blank lines from code pasted from an editor.
- Converting tab-indented code to space-indented for style consistency.
How are options applied?
Options are applied in sequence: tab expansion -> line trimming -> space collapsing -> blank-line handling -> leading/trailing blank line removal. All processing happens locally in your browser.
Unicode whitespace characters
Beyond the familiar ASCII space (U+0020) and tab (U+0009), there are many Unicode whitespace characters that commonly cause invisible formatting problems:
- No-break space U+00A0: looks like a space but prevents line breaks - common in text pasted from web pages.
- En space U+2002 / Em space U+2003: wider spacing characters used in typography.
- Hair space U+200A: a very thin space used in mathematical notation.
- Zero-width space U+200B: invisible but present; can break string matching.
Regular expression for whitespace
The standard regex \s matches only ASCII whitespace in most engines. To also match
Unicode whitespace characters, use a broader pattern:
[\s\u00A0\u2000-\u200B\u202F\u3000] This covers the most common Unicode spaces: no-break space, general punctuation spaces (U+2000–U+200B), narrow no-break space (U+202F), and ideographic space (U+3000, used in CJK typography).
CSV and structured data note
Leading or trailing whitespace in a CSV cell can cause silent failures in data pipelines: SQL
joins on ' Smith' vs 'Smith' will not match; pandas lookups on keys with
invisible trailing spaces return NaN. Always clean whitespace from imported CSV data before processing,
especially when values are used as join keys or dictionary lookups.