Remove Extra Spaces
Clean up your text by removing double spaces, tabs, and trailing whitespace.
Clean up your text by removing double spaces, tabs, and trailing whitespace.
Text copied from documents, web pages, or PDFs often contains invisible whitespace problems — extra spaces between words, tabs, non-breaking spaces, or inconsistent line breaks — that cause issues in code and data processing.
\t characters mixed with spaces characters that look like spaces but aren't for intentional spaces)Excel and Google Sheets have a TRIM() function that removes leading, trailing, and consecutive internal spaces: =TRIM(A1). For non-breaking spaces, combine with SUBSTITUTE: =TRIM(SUBSTITUTE(A1, CHAR(160), " ")). This handles most copy-paste whitespace issues.
To collapse multiple spaces: replace + (two or more spaces) with a single space. To trim: replace ^\s+|\s+$ with empty string. Combined one-liner in JS: str.trim().replace(/\s+/g, ' '). The \s class matches spaces, tabs, and line breaks.
Word processors, PDFs, and web pages use special Unicode whitespace characters beyond regular space (U+0020): non-breaking space (U+00A0), en space (U+2002), em space (U+2003), zero-width space (U+200B), and others. Standard regex \s may not match all of them — use Unicode-aware regex or normalize to ASCII space first.