W3docs

String Functions

Extra Spaces Remover

Collapse runs of whitespace down to a single space. Tabs, newlines, and double-spaces all normalize cleanly.

About the extra spaces remover

This tool collapses runs of two or more consecutive whitespace characters — spaces, tabs, or line breaks — down to a single space. A single stray space or a single tab is left exactly as it is; the tool only acts once whitespace repeats. That distinction matters: text pasted from a Word document, a scraped web page, or a misaligned code sample often has irregular runs of 2, 3, or more spaces where one is expected, and this normalizes all of them to a single space without touching anything else in the string.

Under the hood it’s one regular expression: input.replace(/\s\s+/g, ' '). JavaScript’s \s character class matches spaces, tabs, newlines, carriage returns, and a handful of Unicode whitespace characters, so \s\s+ matches any run of two or more of those in any combination and replaces the whole run with one literal space. The transform runs entirely in your browser — there’s no server round trip, and nothing you paste is sent, logged, or stored. Click Submit to run it; the result doesn’t update on every keystroke.

Because \s matches newlines too, a blank line between paragraphs — really two or more \n characters in a row — also collapses to a single space, merging the paragraphs it separated onto one line. A single newline with no blank line next to it is left alone, so ordinary line-by-line text keeps its structure. Reach for this tool when cleaning up pasted prose or user input before storing it; if you need to strip blank lines while keeping the rest of the line breaks, use the Empty Lines Remover instead.

Examples

Mixed spaces and a lone tabtext
Input:  Too   many    spaces   and\ttabs   in   here.
Output: Too many spaces and\ttabs in here.

Runs of two or more whitespace characters collapse to one space, even when they mix spaces and tabs. The lone tab between “and” and “tabs” is exactly one character, so the pattern (\s\s+) doesn’t match it and it survives untouched.

Blank lines merge paragraphs onto one linetext
Input:
Line one.

Line two.

Line three.

Output:
Line one. Line two. Line three.

A blank line is two or more newline characters back to back — a whitespace run like any other — so it collapses to a single space and the three paragraphs end up on one line. A lone newline with no blank line next to it is left alone.

The equivalent one-liner in JavaScriptjs
// What this tool runs, client-side, on Submit:
input.replace(/\s\s+/g, ' ');

// Same result, written with a quantifier instead of two \s:
input.replace(/\s{2,}/g, ' ');

Only runs of two or more whitespace characters match, so a single space or tab is never touched. PHP’s preg_replace('/\s\s+/', ' ', $s) — the pattern this transform was ported from — behaves the same for ordinary spaces, tabs, and line breaks.

Frequently asked questions

Does removing extra spaces also remove line breaks?

A single line break between two lines is left alone. But a blank line — two or more line breaks in a row — is a whitespace run just like repeated spaces, so it collapses into a single space and the paragraphs it separated get merged onto one line.

Why didn’t a single extra space or tab get removed?

The tool only matches runs of two or more consecutive whitespace characters — the pattern is \s\s+, not \s+. A lone space or a lone tab is a single character, so it never matches and is left exactly as it was; only repeated whitespace gets collapsed.

Is my text uploaded or stored anywhere?

No. The transform is a single JavaScript regular expression that runs in your browser — there’s no API call, no server round trip, and nothing is logged or stored. Reloading the page clears everything.

How is this different from JavaScript’s trim()?

trim() removes all leading and trailing whitespace from a string entirely. This tool doesn’t trim the edges — it collapses a run of 2 or more whitespace characters down to a single space wherever it occurs, so " padded " becomes " padded ", not "padded".

Can I use this to clean up indented code?

Be careful — a line’s leading indentation plus the newline before it forms one whitespace run, so collapsing it pulls the indented line up onto the previous line, separated by a single space. Running this on Python, YAML, or anything where whitespace is meaningful will break the structure — it’s built for prose and plain text, not source code.