Text
Collapse runs of whitespace
Flags: g
Replace multiple consecutive whitespace characters with a single space.
Pattern
Regular expression
\s+How it works
`\s` matches any whitespace (space, tab, newline). `+` means one or more. Combined with a replace, this normalizes messy whitespace to single spaces.
Matches
- ▸` ` (two spaces)
- ▸`\t\n` (tab+newline run)
Non-matches
- ▸`a` (letter, not whitespace)
Language snippets
JavaScript
text.replace(/\s+/g, " ").trim();Python
re.sub(r"\s+", " ", text).strip()Go
re := regexp.MustCompile(`\s+`)
strings.TrimSpace(re.ReplaceAllString(text, " "))Java
text.replaceAll("\\s+", " ").trim()