/^/
EasyText Processing3 min read

Duplicate Spaces Regex

Matches two or more consecutive literal space characters, commonly used to collapse extra spacing down to a single space.

#whitespace#text-processing#spaces#formatting#collapse

Regex Pattern

[ ]{2,}

Default flags: g

Pattern Breakdown

Hover over a token to see what it does.

[ ]{2,}
TokenMeaning
[Opens a character class
The literal space character (U+0020) being matched
]Closes the character class
{2,}Requires two or more consecutive matches of the preceding space character

Detailed Explanation

What it does

This pattern locates runs of two or more literal spaces in a row, ignoring tabs and newlines entirely. It's designed for text-cleanup tasks where accidental double or triple spacing needs to be collapsed to a single space.

Why it works

Using an explicit [ ] character class (rather than \s) keeps the match scoped to the space character only, so tabs and newlines are left untouched. The {2,} quantifier requires at least two spaces before it counts as 'duplicate', so single spaces between words are correctly left alone.

Common use cases

  • Cleaning up user-submitted text before storing or displaying it
  • Normalizing copy-pasted content that often contains inconsistent spacing
  • Pre-processing text for tokenization or search indexing
  • Enforcing single-space style in a linter for prose or documentation files

Edge cases

  • Exactly two spaces, like 'a b', is matched since {2,} has no upper bound
  • A single space, like 'a b', is correctly left unmatched
  • A tab character between words, like 'a\tb', does not count as duplicate spaces since the class only includes literal spaces
  • Leading or trailing double spaces, like ' leading' or 'trailing ', are matched just like duplicate spaces in the middle of text

Limitations

  • This pattern does not catch duplicated whitespace that mixes spaces and tabs, like a space followed by a tab followed by a space
  • It only targets the ASCII space character (U+0020), not other Unicode space-like characters such as non-breaking spaces
  • Collapsing matches to a single space with replace() does not by itself trim leading or trailing spaces from the whole string

Interactive Tester

Edit the pattern or text below — matching runs live in your browser.

a b a b leading
3 matches

Test Cases

Editable — add your own inputs to see if they pass.

InputExpectedResult
Pass
Pass
Pass
Pass
Pass
Pass
Pass
Pass

Language Variants

Production-ready examples in 12 languages.

const duplicateSpacesRegex = /[ ]{2,}/g;
const cleaned = 'too   many    spaces'.replace(duplicateSpacesRegex, ' ');

Common Mistakes

Using \s{2,} instead of [ ]{2,}, which unintentionally also matches runs mixing tabs and newlines with spaces

Fix: Use an explicit [ ] character class when only literal spaces should count as duplicates, or \s{2,} deliberately if any whitespace mix should collapse

Replacing matches with an empty string instead of a single space, which merges words together

Fix: Replace with ' ' (a single space) to collapse runs down to one separator instead of deleting it entirely

Forgetting the global flag, so only the first run of duplicate spaces in the string gets collapsed

Fix: Add the g flag so replace() collapses every duplicate-space run in the string

Performance Notes

  • The {2,} bounded-below quantifier on a single-character class is very cheap and matches in linear time
  • Because the character class has exactly one member, the engine can often optimize this into a simple scan rather than general backtracking logic
  • For very large documents, streaming/chunked replacement may be preferable to a single huge regex replace call

Browser Compatibility

EngineSupportedNotes
ChromeYes
FirefoxYes
SafariYes
EdgeYes
Node.jsYes