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
| \s | one 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.
| Input | Result | Matched |
|---|---|---|
| a b | matches | |
| a\tb | matches | \t |
| a\n\nb | matches | \n\n |
| ab | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to trim leading and trailing whitespace
- Regex for a UUID
- Regex for a hex colour code