Xerobit

Whitespace Remover: Trim, Collapse and Clean Text

This whitespace remover cleans up text by trimming lines, collapsing multiple spaces into one, removing blank lines and stripping invisible zero-width characters. Every option runs locally in your browser so nothing is uploaded to a server. Paste your text, pick the transformations you need and copy the result.

Result

How to use the whitespace remover

  1. Paste or type your text into the input area.
  2. Check the boxes for the operations you want: trim lines, collapse spaces, remove blank lines, tabs to spaces, remove all whitespace or strip zero-width characters.
  3. The result updates instantly as you change options.
  4. Copy the cleaned text from the output box.

What counts as whitespace

In computing, whitespace includes spaces, tabs (\t), line feeds (\n), carriage returns (\r) and several Unicode space characters such as the non-breaking space (U+00A0) and the em space (U+2003). Zero-width characters like U+200B (zero-width space), U+200C (zero-width non-joiner), U+200D (zero-width joiner) and U+FEFF (byte order mark) are invisible but still occupy code points in a string.

Python's re.sub(r'\s+', '', text) removes every character matched by \s, which covers spaces, tabs, newlines and Unicode spaces for str patterns. This whitespace remover uses JavaScript's \s, which covers almost the same set. The two differ on a few characters: JavaScript also matches U+FEFF, while Python also matches U+001C to U+001F and U+0085.

How each option works

The transformations run in a fixed order so they compose predictably:

  1. Tabs to spaces: replaces each tab with N spaces (the tab size you set).
  2. Strip zero-width characters: deletes U+200B, U+200C, U+200D and U+FEFF.
  3. Remove all whitespace: if checked, deletes every space, tab and newline using the pattern /\s/g. When this is active the remaining options are skipped because there is no whitespace left to process.
  4. Trim lines: removes leading and trailing spaces, tabs, non-breaking spaces and other Unicode spaces from every line individually.
  5. Collapse spaces: replaces every run of two or more spaces, tabs or other horizontal whitespace characters inside a line with a single space. A lone tab is left alone.
  6. Remove blank lines: drops every line that is empty or contains only whitespace.

For example, given the input " hello world " with trim and collapse enabled, step 4 produces "hello world" and step 5 produces "hello world".

Common use cases

Developers and writers use a whitespace remover for several everyday tasks:

  • Cleaning pasted text: content copied from PDFs or word processors often carries irregular spacing, non-breaking spaces and hidden formatting characters. Collapsing spaces and stripping zero-width characters restores normal text.
  • Preparing data for scripts: CSV rows or log lines with inconsistent spacing can break parsers. Trimming lines ensures uniform fields.
  • Minifying snippets: removing all whitespace is useful when you need a compact representation of a JSON key, a CSS selector or a URL path.
  • Normalising indentation: converting tabs to spaces at a consistent width avoids alignment issues when sharing code across editors.

If you also need to count what remains, pair this tool with a character counter or a word counter to verify lengths after cleaning.

Reference: whitespace characters handled

CharacterUnicodeDescription
SpaceU+0020Standard ASCII space
TabU+0009Horizontal tabulation
Line feedU+000ANewline on Unix and macOS
Carriage returnU+000DUsed with LF on Windows
Non-breaking spaceU+00A0Prevents line wrapping
Zero-width spaceU+200BInvisible break opportunity
Zero-width non-joinerU+200CPrevents ligatures
Zero-width joinerU+200DForces ligatures or emoji sequences
BOM / ZWNBSPU+FEFFByte order mark or zero-width no-break space

Whitespace remover vs Python re

A frequent question is whether this tool matches Python's re.sub(r'\s+', ' ', text).strip(). The answer depends on which options you enable. To replicate that exact Python call, check trim lines and collapse spaces while leaving remove all whitespace unchecked. Both Python's \s and JavaScript's /\s/ match U+00A0 and the other Unicode spaces, so the results agree for ordinary text.

If you need to remove extra spaces from text that was generated programmatically, the collapse option turns each run of spaces, tabs or form feeds into one space, like Python's \s+ replacement, except that it never joins separate lines. The main difference is that this tool lets you toggle each transformation independently rather than writing separate regular expressions for each step.

Worked examples

ExampleInputResult
Collapse extra spacesText: Hello world from Xerobit; Trim leading and trailing spaces on each line: yes; Collapse multiple spaces into one: yes; Remove all whitespace (spaces, tabs, newlines): no; Remove blank lines: no; Convert tabs to spaces: no; Tab size: 4; Strip zero-width characters: noHello world from Xerobit
Remove blank linesText: Line one / Line two / Line three; Trim leading and trailing spaces on each line: yes; Collapse multiple spaces into one: yes; Remove all whitespace (spaces, tabs, newlines): no; Remove blank lines: yes; Convert tabs to spaces: no; Tab size: 4; Strip zero-width characters: noLine one / Line two / Line three
Tabs to spacesText: function test() { / return true; / }; Trim leading and trailing spaces on each line: no; Collapse multiple spaces into one: no; Remove all whitespace (spaces, tabs, newlines): no; Remove blank lines: no; Convert tabs to spaces: yes; Tab size: 2; Strip zero-width characters: nofunction test() { / return true; / }
Remove all whitespaceText: a b c / d e; Trim leading and trailing spaces on each line: no; Collapse multiple spaces into one: no; Remove all whitespace (spaces, tabs, newlines): yes; Remove blank lines: no; Convert tabs to spaces: no; Tab size: 4; Strip zero-width characters: noabcde

FAQ

Does this whitespace remover upload my text?

No. All processing happens in your browser using JavaScript. Your text never leaves your device.

How do I remove extra spaces between words?

Enable the Collapse multiple spaces into one option. It replaces any run of two or more spaces, tabs or other horizontal whitespace characters with a single space; line breaks are kept.

What does Remove all whitespace do differently from the other options?

It deletes every space, tab and newline character entirely, producing a single continuous string. The other options preserve line structure and only adjust spacing.

Which zero-width characters are stripped?

The tool removes U+200B (zero-width space), U+200C (zero-width non-joiner), U+200D (zero-width joiner) and U+FEFF (byte order mark). These are invisible characters that often sneak in when copying from web pages or rich text editors.

Can I convert tabs to a specific number of spaces?

Yes. Check Tabs to spaces and set the Tab size field to any value from 1 to 16. Each tab character is replaced with that many space characters.

Is this the same as Python's str.strip()?

Python's strip() removes leading and trailing whitespace from the whole string. The Trim lines option applies the same idea to every individual line, like reading a file line by line and calling strip() on each.

How do I remove whitespace from text that has mixed line endings?

The tool accepts LF (Unix), CRLF (Windows) and lone CR line endings, and the output always uses LF. Blank line removal and trimming work regardless of the input style.