← Back to all regex patterns
Replace
Low backtracking risk

Regex to Replace Repeated Whitespace

Use this before `.replace()` when normalizing messy copy, scraped text, or imported CSV fields with inconsistent spacing.

Regex
/\s+/g

Try this pattern

Matches current input
too many spaces

2 matches found in the current text.

Passing examples

  • too many spaces
  • line breaks count too

Failing examples

  • single-space
  • clean

Code examples

JavaScript
const regex = /\s+/g;
regex.test(input);
Python
import re
pattern = re.compile(r"\s+")
bool(pattern.search(input))
Go
re := regexp.MustCompile("\\s+")
matched := re.MatchString(input)

Related Replace Patterns