Skip to main content

Whitespace Remover

Trim, collapse or remove whitespace and blank lines from text.

Reviewed by · Last reviewed

How to Use the Whitespace Remover

  1. Paste your text into the input area. Any mix of spaces, tabs, blank lines, or line endings is accepted.
  2. Pick a cleaning mode from the radio-style buttons above the output: Trim Edges, Collapse Spaces, Remove All Spaces, Remove Blank Lines, or Remove All Whitespace.
  3. Watch the output update live as you switch modes - no submit button required.
  4. Check the byte counts shown beside the input and output. The delta tells you exactly how many characters the mode removed.
  5. Click Copy to send the cleaned text to your clipboard, ready to paste into your editor, CMS, or database row.
  6. Switch modes and re-copy freely; the original input is never mutated, so you can try several passes.

What Each Mode Actually Does

Trim Edges runs line.trim() on each line, which strips all Unicode whitespace characters defined by \\s from both ends. Collapse Spaces applies the regex /[ \\t]+/g and replaces each run with a single space, leaving newlines untouched. Remove All Spaces uses /[ \\t]+/g with an empty replacement so words collide. Remove Blank Lines filters the split line array with a predicate line.trim().length > 0. Remove All Whitespace applies /\\s+/g globally, which matches every character in Unicode category Zs plus line terminators (\\n, \\r, \\t, \\v, \\f). All modes use String.prototype.replace, so the transformations are linear in the length of the input and run in the browser's native regex engine with no extra allocations per character.

When to Use It

  • Fitting text into a character-count-limited field - Twitter, SMS, LinkedIn headline, meta description - by collapsing double spaces.
  • Cleaning up a code snippet pasted from a PDF or slide where every line has leading whitespace that breaks Markdown rendering.
  • Removing blank lines from a CSV before importing so the database does not create a row full of nulls.
  • Flattening a multi-line string into a single line before pasting into a cell of a spreadsheet or the body of a shell one-liner.
  • Preparing email body text for a mailto: link where encoded spaces and newlines cause clients to misrender.
  • Stripping indentation from a code block before running word-count or similarity tools that inflate numbers due to leading tabs.

Common Pitfalls and Edge Cases

  • Non-breaking space (U+00A0). It is a whitespace character visually but some regex engines exclude it from \\s. JavaScript's modern \\s does include it, but the older RegExp tables in legacy browsers may not. Check your output if you are on Internet Explorer 11.
  • Zero-width spaces (U+200B). These are not whitespace per Unicode; they are format characters. Use the invisible-character detector on this site to strip them - this tool will leave them alone.
  • CRLF line endings. Remove All Whitespace removes the \\r and \\n together, flattening the file. Remove Blank Lines preserves line endings but may leave a stray \\r on the last non-empty line if you pasted DOS-format text.
  • YAML and Python indentation. Trim Edges and Remove All Spaces will happily break these languages because indentation is syntactically significant. Use Collapse Spaces at most, and double-check the output.
  • Trailing newline. Many systems (Unix tools, Git, editors) expect a final newline. Remove Blank Lines preserves the last non-empty line's trailing newline if you had one; Remove All Whitespace discards it.
  • Tabs inside code. Collapse Spaces replaces tabs with a single space, which changes alignment in languages that care about column positioning.

What Counts as Whitespace

Unicode defines whitespace via the White_Space property, which covers 25 code points: the ASCII block U+0009 through U+000D plus U+0020, the no-break space U+00A0, the Ogham space mark U+1680, the en quad and related punctuation spaces U+2000 through U+200A, line separator U+2028, paragraph separator U+2029, narrow no-break space U+202F, medium mathematical space U+205F, and ideographic space U+3000. JavaScript's \\s in Unicode-aware mode matches all of these (ECMA-262 defines it as WhiteSpace or LineTerminator production). POSIX regex and older grep use a narrower definition limited to ASCII. Knowing this matters when your text comes from a Japanese source and uses U+3000 between words, or from a typography-heavy PDF that sprinkles en spaces between clauses.

Comparison to Alternatives

On the command line, tr -s ' ' collapses runs of spaces, sed 's/^[ \\t]*//;s/[ \\t]*$//' trims each line, and awk 'NF' removes blank lines. All three compose well in pipelines and run at native speed on gigabytes of text. Your IDE's "Trim Trailing Whitespace on Save" setting is the right long-term fix for code files. VS Code and JetBrains IDEs also have "Delete Blank Lines" commands in their menus. Dedicated formatters such as prettier, black, and gofmt normalize whitespace as part of a broader reformat. Use this web tool when you have one text snippet that needs cleanup, you are not in a shell, and you want to preview modes side by side before committing to one - especially on mobile or on a machine where you cannot install anything.

Frequently Asked Questions

What is the difference between Remove All Spaces and Remove All Whitespace?

Remove All Spaces strips only regular spaces (U+0020) and tabs (U+0009), leaving line breaks intact so your text keeps its original line structure. Remove All Whitespace goes further and also strips line feeds (U+000A), carriage returns (U+000D), vertical tabs, form feeds, and the full set of Unicode space separators, producing a single continuous string with no breaks anywhere. The first is for fitting into narrow columns; the second is for creating keys or hashes where only the visible characters should matter.

Does Collapse Spaces also collapse mixed tab-and-space runs?

Yes. The regex matches any run of one-or-more characters from the class [ \t], so a mix like tab-space-space-tab is treated as one run and replaced with a single space. This is what you usually want when text came from a word processor that freely alternated between the two for alignment, but it is destructive if you care about the original indentation style.

Is any of this processing done on a server?

No. Every mode is a plain JavaScript regex replacement executed inside the browser tab on the main thread. There is no fetch, no worker, no service worker interception, and no telemetry. Disable your network after the page loads and every mode continues to work identically. The tool works identically offline on any modern browser that supports the basic String and RegExp APIs from ES2015.

Will this break my YAML or Python code?

It can, catastrophically. Both languages use leading whitespace as syntax - Python indents blocks with consistent spaces or tabs, and YAML nests structures with indentation. Trim Edges, Remove All Spaces, and Remove All Whitespace will all produce broken code. Collapse Spaces leaves leading whitespace alone within the line break semantics but flattens runs. If the input is code, run the file through a proper formatter (black, prettier, gofmt) rather than this tool.

What happens to CRLF line endings?

Remove All Whitespace strips both the carriage return and the line feed, flattening everything. Collapse Spaces and Remove All Spaces leave line endings untouched. Remove Blank Lines treats a line that contains only \r as blank (because trim removes it) and filters it out, which is usually what you want after opening a Windows-edited file on a Unix tool. If you need to convert line endings specifically, a tool like dos2unix does that more precisely.

Why does my character count drop by more than I expect after Remove Blank Lines?

Every blank line in the input contains at least a single line-feed character, and sometimes a carriage return as well. Removing 50 blank lines on a CRLF file drops the count by 100 characters even though you did not see anything go. The byte-count display reflects the raw character count, so the delta matches what a hex editor would show.

Does Trim Edges strip leading BOM characters?

Yes, if the BOM is the first character of the first line. The U+FEFF byte-order mark is matched by \s in modern JavaScript engines, so trim() removes it. This is a useful side effect when cleaning up a file exported from Notepad that introduced a BOM that breaks downstream parsers. For dedicated invisible-character cleanup, the invisible-character detector on this site reports every occurrence and gives finer control.

Can I chain multiple modes?

The UI exposes one mode at a time, but you can pipe your text through multiple passes: pick Remove Blank Lines, copy the output, paste it back, then pick Trim Edges. Two-pass pipelines cover most real cleanups. For programmatic chaining, a small script calling String.prototype.replace several times is simpler than trying to build one mega-regex.

Does it preserve emoji and non-Latin characters?

Yes. The modes operate on whitespace characters specifically; everything else passes through untouched. Emoji, CJK ideographs, Arabic, Hebrew, Devanagari, and any other script are preserved byte-for-byte. The one caveat is the ideographic space U+3000 used in Japanese typography, which Remove All Whitespace will strip because it is part of Unicode's whitespace set.

Is there a size limit?

The practical limit is what your browser will happily render in a textarea: a few megabytes before scroll latency becomes noticeable. The regex replacement itself is linear in the input length and handles much more. For log files in the tens of megabytes range, a command-line tool (sed, awk, tr) finishes faster and does not tie up your browser tab.

More Text Tools