All guides
Text

Remove extra spaces without losing line breaks

Tidy a pasted draft while keeping its paragraphs. The pattern and the replacement both matter.

You paste a draft from another app. The words have extra spaces between them, but the paragraphs are worth keeping. Replacing all whitespace with a single space can turn the whole draft into one long line.

For ordinary repeated spaces and tabs, start with this pattern in the regex playground:

[ \t]{2,}

Replace each match with one ordinary space, with Find all matches enabled. This pattern does not include line breaks.

Try it on a small draft first

Download the sample text. It contains repeated spaces, two tab characters after the second label, the name Zoë and a blank line before the last paragraph.

  1. Paste the file's contents into Test text.
  2. Paste the pattern above into Regular expression, without surrounding slashes.
  3. In Options, make sure Find all matches is on. The flag shown beside the expression should include g.
  4. Choose Replace. On a phone, open Results first.
  5. In Replace with, type one space. Leaving this field empty would delete the matched spacing instead.

The preview should show three matches replaced and keep this layout:

Creator: Zoë
Topic: Colour palettes

Next step: export PDF.

Compare it with the cleaned text file before copying the output into your draft.

Why the broader pattern loses paragraphs

In JavaScript regular expressions, the whitespace class also includes line terminators. This tempting pattern matches more than spaces:

\s+

With a space as the replacement, it also replaces the newlines and the blank paragraph separator. The flattened output shows the difference. That can be useful for a deliberately single-line value, but it is the wrong choice when paragraphs should remain.

The narrower pattern matches runs of at least two ordinary spaces or tabs. It leaves a single tab unchanged. If you want to turn single tabs into spaces too, use this instead:

[ \t]+

Neither version matches every possible Unicode spacing character. A non-breaking space copied from a webpage may need separate handling. Check what is actually in the input instead of making the pattern broader until it seems to work.

Check the match count and replacement

Without the g flag, only the first matching run is replaced. If the first line looks fixed but later lines still have gaps, check Find all matches before changing the pattern.

Keep the original nearby and compare the result. Tabs and spaces can carry meaning in tables, code, poetry and Markdown indentation. A whitespace cleanup is suitable for this plain-text draft; it is not a safe universal formatter for every file.

For Markdown, use the Markdown editor to check the rendered structure after editing. For structured data, use a formatter that understands that format.

What we checked

On 5 October 2026, we passed the sample through the same JavaScript matching function as the playground. The narrow pattern replaced three runs and preserved every newline, including the empty line. We also checked the broad pattern and the first-match-only case. The results file records the patterns, flags and exact outputs.

The tool processes text in your browser. These results describe the supplied sample, not every document that happens to contain repeated spaces.