How to use the whitespace remover
- Paste or type your text into the input area.
- 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.
- The result updates instantly as you change options.
- 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:
- Tabs to spaces: replaces each tab with N spaces (the tab size you set).
- Strip zero-width characters: deletes U+200B, U+200C, U+200D and U+FEFF.
- 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. - Trim lines: removes leading and trailing spaces, tabs, non-breaking spaces and other Unicode spaces from every line individually.
- 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.
- 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
| Character | Unicode | Description |
|---|---|---|
| Space | U+0020 | Standard ASCII space |
| Tab | U+0009 | Horizontal tabulation |
| Line feed | U+000A | Newline on Unix and macOS |
| Carriage return | U+000D | Used with LF on Windows |
| Non-breaking space | U+00A0 | Prevents line wrapping |
| Zero-width space | U+200B | Invisible break opportunity |
| Zero-width non-joiner | U+200C | Prevents ligatures |
| Zero-width joiner | U+200D | Forces ligatures or emoji sequences |
| BOM / ZWNBSP | U+FEFF | Byte 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
| Example | Input | Result |
|---|---|---|
| Collapse extra spaces | Text: 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: no | Hello world from Xerobit |
| Remove blank lines | Text: 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: no | Line one / Line two / Line three |
| Tabs to spaces | Text: 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: no | function test() { / return true; / } |
| Remove all whitespace | Text: 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: no | abcde |
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.