Whitespace Character Regex
Matches one or more consecutive whitespace characters, including spaces, tabs, newlines, carriage returns, form feeds, and vertical tabs.
Regex Pattern
[ \t\n\r\f\v]+Default flags: g
Pattern Breakdown
Hover over a token to see what it does.
| Token | Meaning |
|---|---|
| [ | Opens a character class listing every whitespace character to match |
| A literal space character (U+0020) | |
| \t | A horizontal tab character |
| \n | A line feed (Unix newline) character |
| \r | A carriage return character |
| ]+ | Closes the character class and requires one or more consecutive matches, so runs of whitespace are matched as a single unit |
Detailed Explanation
What it does
This pattern finds runs of whitespace in a string by explicitly listing each whitespace character it should match: spaces, tabs, newlines, carriage returns, form feeds, and vertical tabs. With the global flag, it locates every whitespace run in the input rather than just the first one.
Why it works
An explicit character class lists exactly the characters that should count as whitespace for this pattern, which is more predictable across regex engines than relying on the built-in \s shorthand (whose exact Unicode coverage varies slightly by language and engine). The + quantifier groups consecutive whitespace characters into one match, which is useful for both detection and collapsing/trimming operations.
Common use cases
- Splitting a string into words with String.split(/[ \t\n\r\f\v]+/)
- Detecting whether a string contains any whitespace before rejecting it as an identifier
- Normalizing user input by replacing all whitespace runs with a single space
- Stripping whitespace-only lines out of a text file during preprocessing
Edge cases
- Unicode whitespace like a non-breaking space (U+00A0) is intentionally NOT matched, since the class lists only the common ASCII whitespace characters
- A string made entirely of whitespace, like three spaces, still matches as one run
- Mixed whitespace runs, such as a tab followed by two spaces, are matched as a single combined run because every character in the run belongs to the class
- An empty string never matches since + requires at least one character
Limitations
- This explicit class treats all listed whitespace types the same, so it cannot distinguish tabs from spaces from newlines on its own
- It does not by itself trim leading/trailing whitespace; that requires anchors like ^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$
- It omits some Unicode space separators (e.g. U+2028, U+2029, U+00A0) that the built-in \s shorthand does include in most engines
Interactive Tester
Edit the pattern or text below — matching runs live in your browser.
Test Cases
Editable — add your own inputs to see if they pass.
| Input | Expected | Result | |
|---|---|---|---|
| Pass | |||
| Fail | |||
| Pass | |||
| Pass | |||
| Pass | |||
| Fail | |||
| Pass | |||
| Pass |
Language Variants
Production-ready examples in 12 languages.
const whitespaceRegex = /[ \t\n\r\f\v]+/g;
const words = 'hello world\tfoo'.split(whitespaceRegex);Common Mistakes
Using a literal space character ' ' when the goal is to match all whitespace types, missing tabs and newlines
Fix: List every whitespace character explicitly in a class, e.g. [ \t\n\r\f\v], or use \s if broad Unicode coverage is desired
Forgetting the + quantifier and matching whitespace one character at a time, which is inefficient for collapsing runs
Fix: Use the class with + so consecutive whitespace is treated as a single match, especially when replacing with a single space
Assuming this pattern trims a string, when it only matches whitespace anywhere in it
Fix: Use dedicated trim methods (String.trim()/.trim()) or anchor with ^ and $ plus the g flag for manual trimming
Performance Notes
- An explicit whitespace character class is one of the cheapest regex operations available, with no backtracking risk
- Prefer native String.trim()/.strip() methods over regex for simple leading/trailing whitespace removal, since they're typically faster
- When splitting large text on whitespace, the global flag with split() avoids manual index tracking and remains linear in input size
Browser Compatibility
| Engine | Supported | Notes |
|---|---|---|
| Chrome | Yes | — |
| Firefox | Yes | — |
| Safari | Yes | — |
| Edge | Yes | — |
| Node.js | Yes | — |