Remove Extra Spaces
Collapse doubled spaces, tabs and non-breaking spaces to one space and trim every line, while keeping your line breaks exactly where they are.
The interactive tool loads here when JavaScript runs.
About this toolWhat is Remove Extra Spaces?
Remove Extra Spaces cleans whitespace inside each line. Any run of spaces, tabs or non-breaking spaces becomes a single ordinary space, and spaces at the start and end of every line are removed. Line breaks are kept, so a list stays a list and paragraphs stay separate.
It is the fix for text pasted from PDFs, web pages, Word documents and spreadsheets, which often carries doubled spaces, tab-aligned columns and invisible non-breaking spaces (U+00A0) that break searches and comparisons.
Empty lines are kept (a line of only spaces becomes an empty line). Run Remove Empty Lines afterwards if you want them gone. Processing happens in your browser.
- Collapses spaces, tabs and non-breaking spaces to one space
- Trims the start and end of every line
- Keeps line breaks and paragraph structure
- Copy and Clear buttons
- Runs in your browser; nothing is uploaded
Updated
How to use Remove Extra Spaces
- Paste your text into Input Text.
- Click Clean Spaces.
- Check Output Text: each line has single spaces between words and no leading or trailing spaces.
- Click Copy Result, or Clear to start over.
Worked example
Cleaning text copied from a PDF invoice
Input
Invoice number: 4521 Due date: 30 Nov
Output
Invoice number: 4521 Due date: 30 Nov
The tabs and doubled spaces collapse to one space, the leading and trailing spaces disappear, and the line break between the two fields is preserved.
Background and insight
Text has more kinds of space than most people expect. Besides the ordinary space (U+0020) and tab (U+0009), Unicode defines the non-breaking space (U+00A0), thin and en spaces (U+2009, U+2002) and others. Word processors and HTML insert non-breaking spaces to keep "10 kg" on one line, and the entity produces one. They look identical to a normal space, but "10 kg" with U+00A0 is not equal to "10 kg" with U+0020, which is why a spreadsheet lookup or a text search sometimes fails for no visible reason.
This tool uses JavaScript's \s character class, which covers all of those Unicode space separators, but deliberately excludes line feed and carriage return so the line structure survives. The equivalent sed command is sed -E 's/[[:space:]]+/ /g; s/^ //; s/ $//', though POSIX [[:space:]] does not match U+00A0 in most locales.
Collapsing whitespace is lossy. Indentation in code, YAML and Python is meaningful, and tab-separated values use tabs as delimiters. Do not run this on those formats unless you intend to flatten them.
Common use cases
Cleaning PDF and web copy
Remove doubled spaces and tab runs from text copied out of PDFs, slides and web pages.
Fixing lookup mismatches
Strip trailing and non-breaking spaces from IDs or names before using them in a VLOOKUP, a join or a dedupe.
Preparing lists for comparison
Normalise spacing before removing duplicates or comparing two lists, so "a b" and "a b" match.
Tidying form input
Clean addresses and names pasted from emails before entering them into a CRM.
Tips and limitations
- Do not use this on Python, YAML or Makefiles: indentation is part of the syntax there.
- Tab-separated data loses its columns, because tabs become spaces. Convert TSV to CSV first if you need to keep the fields.
- Lines that contained only spaces become empty lines. Follow up with Remove Empty Lines to drop them.
- Zero-width characters such as U+200B are not whitespace in JavaScript's definition, so they survive. If two strings still do not match after cleaning, suspect a zero-width space.
Sources and further reading
- Character classes, MDN Web Docs. What JavaScript's \s matches, including U+00A0 and other Unicode spaces
- ECMAScript Language Specification: White Space, Ecma International (TC39). The code points JavaScript treats as white space
- Regular Expressions, The Open Group (POSIX). Bracket expressions such as [[:space:]]
Frequently asked questions
Does Remove Extra Spaces keep my line breaks?
Yes. Spaces and tabs are collapsed within each line, and each line is trimmed, but line breaks are left where they are. A list of 20 lines comes out as 20 lines. Lines that held only whitespace become empty lines, which you can remove with Remove Empty Lines.
How do I remove non-breaking spaces from text?
Paste the text here and click Clean Spaces. Non-breaking spaces (U+00A0, the character) count as whitespace, so they are replaced with a normal space and collapsed with any neighbours. This often fixes spreadsheet lookups and searches that fail even though the text looks identical.
Why do two strings still not match after removing spaces?
Something other than a space differs. Common culprits are zero-width spaces (U+200B), curly versus straight quotes, a Cyrillic letter that looks Latin, or different Unicode normalisation of accented letters. Paste both strings into the Online Diff Tool to see exactly where they differ.
Can I remove all spaces instead of collapsing them?
Not with this tool, which always leaves one space between words. To delete every space, use the Find tab of the Online Text Processor: put a single space in Find, leave Replace with empty and click Find & Replace All. Tabs need a second pass.
Will this tool break code indentation?
Yes, it removes leading whitespace from every line, so indentation disappears. That breaks Python, YAML and Makefiles, where indentation is syntax, and makes other code harder to read. Use a formatter for the language instead, such as the JSON or SQL formatters on this site.
Is there an in-depth guide to Remove Extra Spaces?
Yes. “Cleaning Up Text Lists: Spaces, Blank Lines, Duplicates and Commas” covers the background, how it works, common pitfalls and worked examples. Read the full guide.
Related Tools You May Like
Free, in-browser tools that pair well with this one.