W3docs

String Functions

Extra Spaces Remover

Collapse runs of spaces and tabs down to a single space and trim each line. Line breaks are kept, and long runs of blank lines shrink to one blank line.

About the extra spaces remover

This tool cleans up irregular whitespace without flattening your text. Runs of horizontal whitespace — spaces, tabs and non-breaking spaces — collapse to a single space, and each line is trimmed of leading and trailing whitespace. A lone tab is also turned into a space. Text pasted from a Word document, a scraped web page, or a misaligned sample often has runs of two, three or more spaces where one is expected, and this normalizes all of them.

Line breaks are kept. Windows line endings are converted to \n, single line breaks stay as they are, and three or more consecutive line breaks (including lines holding only spaces) collapse to one blank line, so paragraphs stay separated. The transform runs entirely in your browser — nothing you paste is sent, logged, or stored.

Reach for it when cleaning pasted prose or user input before storing it. Because it trims every line, it removes indentation, so don't run it on Python, YAML or other code where leading whitespace is meaningful. To remove blank lines entirely instead of reducing them to one, use the Empty Lines Remover.

Examples

Mixed spaces and a tab, edges trimmedtext
Input:  "  Too   many    spaces   and\ttabs   in   here.  "
Output: "Too many spaces and tabs in here."

Runs of spaces and tabs become a single space, and the leading and trailing spaces are trimmed.

Paragraphs and line breaks are kepttext
Input:
Line one.



Line two.   
   Line three.

Output:
Line one.

Line two.
Line three.

Four line breaks in a row become one blank line, and each line is trimmed. Paragraphs are never merged.

The same cleanup in JavaScriptjs
text
  .replace(/\r\n?/g, '\n')
  .replace(/[^\S\n]+/g, ' ')
  .split('\n').map((l) => l.trim()).join('\n')
  .replace(/\n{3,}/g, '\n\n');

[^\S\n] matches any whitespace except a line feed, which includes tabs and non-breaking spaces.

Frequently asked questions

Does removing extra spaces also remove line breaks?

No. Line breaks are kept and paragraphs stay separate. Only runs of three or more line breaks are reduced to a single blank line, and whitespace-only lines count as blank.

Does a single extra space or tab get removed?

A single tab is turned into one space, and a single space is left as it is. Spaces at the start or end of a line are always trimmed, even when there is only one.

Is my text uploaded or stored anywhere?

No. The transform runs in your browser — there is no API call and nothing is logged or stored. Reloading the page clears everything.

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

trim() only removes whitespace at the two ends of the whole string. This tool also trims every individual line and collapses repeated whitespace inside lines.

Can I use this to clean up indented code?

Not safely. Every line is trimmed, so indentation is removed — running this on Python, YAML or anything where leading whitespace matters will break the structure. It is built for prose and plain text.