Match separators once, or use + when a whole run of whitespace matters.
Start with /\s/ to ask “is there any whitespace?”
Add + for runs: /\s+/ matches one or more separators.
Use split(/\s+/) to break text into words.
Use replace(/\s+/g, " ") to normalize spacing.
Piece
Required?
Role
\s in the pattern
Yes
Match one whitespace character
+ / *
Optional
Match a run (or optional run) of whitespace
g flag
Optional
Replace every whitespace run
Inverse
—
\S matches non-whitespace
Pseudocode
Pseudocode
pattern has "\s"
read next character ch from the string
if ch is whitespace: // space, tab, newline, …
accept match
else:
fail this path
common actions:
test(str) → true if any whitespace exists
split(/\s+/) → words between whitespace runs
replace(/\s+/g, " ") → collapse runs to one space
Reference
Cheat Sheet
Keep these patterns handy for everyday text cleanup.
Topic
Detail
One whitespace
\s
Whitespace run
\s+
Split words
split(/\s+/)
Collapse gaps
replace(/\s+/g, " ")
Trim edges
str.trim() (or /^\s+|\s+$/g)
Opposite
\S
Standard
ES3+ (universal)
Try it
Live Preview
Run common \s operations on sample strings with mixed whitespace.
Browser demo of the RegExp \s whitespace escape — same ideas as the examples below.
Result
text = "hello"
/\s/.test(text)
→ false
/* no whitespace present */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Detect Whitespace
Return true when space, tab, or newline appears anywhere in the text.
Find every run. The g flag walks the whole string.
Match with \s+. Multiple spaces or tabs count as one run.
Replace with one space. Each run becomes a single " ".
Tabs normalize too. Horizontal tabs become regular spaces in the result.
Edge Cases & Pitfalls
Check these before relying on \s in real projects.
split
Leading or trailing \s+ creates empty tokens
" a b ".split(/\s+/) yields empty strings at the ends. Trim first, or filter empty entries.
literal
A literal space is narrower than \s
/ / matches only the space bar. Tabs and newlines need \s (or an explicit escape).
null
\s does not match NUL
The null character is not whitespace. Use \0 / \x00 when you need to find null bytes.
trim
Prefer trim() for simple edges
For leading/trailing cleanup alone, str.trim() is clearer than /^\s+|\s+$/g.
Compatibility
Browser & Runtime Support
The \s whitespace escape is core regex syntax (ES3+) and works everywhere JavaScript runs.
✓ Baseline · ES3+
RegExp \s
Supported in every modern and legacy browser, plus all Node.js versions. No polyfill required.
100%Universal support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
\sExcellent
Bottom line: Safe to use in any JavaScript environment. Pair with \S when you need the non-whitespace side.
Analysis
Performance Notes
Approach
Cost
Best for
/\s/.test(str)
O(n)
Quick “has whitespace?” checks
str.split(/\s+/)
O(n)
Tokenizing messy text
str.replace(/\s+/g, " ")
O(n)
Normalizing spacing
Character-class style matches are cheap. Prefer \S when you want the visible parts, and \s when you want the separators.
Remember
Key Takeaways
Role:\s matches one whitespace character (space, tab, newline, …).
Runs: use \s+ to match or replace whole gaps.
Split:split(/\s+/) turns mixed spacing into words.
Inverse:\S matches everything that is not whitespace.
One line:\s finds whitespace — detect it, split on \s+, or collapse runs with replace(/\s+/g, " ").
Frequently Asked Questions
\s matches any single whitespace character: space, tab (\t), newline (\n), carriage return (\r), form feed (\f), and vertical tab (\v). Modern engines also treat several Unicode separators as whitespace.
\s matches whitespace. \S is the inverse and matches any character that is not whitespace—letters, digits, and punctuation.
No. A literal space matches only the space character. \s matches space plus tab, newline, and other whitespace types.
Use split with a quantifier: str.split(/\s+/). Prefer trim() first if leading or trailing whitespace would create empty array entries.
No. The null character (\0, U+0000) is not whitespace in JavaScript.
The \s whitespace shorthand is core regex syntax since ES3. It works in every browser and Node.js version.