RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to match whitespace

/\s+/g

\s covers the space, tab, carriage return, newline, vertical tab and form feed. In JavaScript it also covers the non-breaking space and other Unicode spaces; in Python it does under a str pattern but not under bytes.

What each part does

\sone whitespace character
+one or more, so a run collapses into a single match

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
a bmatches
a\tbmatches\t
a\n\nbmatches\n\n
abno match

The mistake to avoid

Using ' ' (a literal space) and wondering why tab-separated input is untouched.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers