The \s metacharacter matches any single whitespace character: spaces, tabs, newlines, and related separators. It is one of the most practical regex tools for parsing text, normalizing spacing, and splitting words.
01
Whitespace
\s
02
Includes
Space, tab, LF
03
Inverse
\S
04
Quantifiers
\s+
05
Split
split(/\s+/)
06
Normalize
replace(/\s+/g, " ")
Fundamentals
Introduction
Text from users, files, and APIs often contains inconsistent whitespace—multiple spaces, tabs, or line breaks. The \s metacharacter gives you a single shorthand to match all common separator types instead of listing each one manually.
One \s matches exactly one whitespace character. Add + to match runs of whitespace, or use split(/\s+/) to break text into words regardless of how it is spaced.
Concept
Understanding the \s Metacharacter
In JavaScript, \s is a predefined character class escape that matches these whitespace characters:
Pair \s with anchors (^, $), quantifiers (+, *), and the global flag for bulk replacement.
Foundation
📝 Syntax
JavaScript
\s // one whitespace character
\s+ // one or more whitespace chars
\s* // zero or more whitespace chars
/\s/g // global: every whitespace char
\S // one non-whitespace (inverse)
Common patterns
/\s+/ — match a run of whitespace (gap between words).
split(/\s+/) — split text on any whitespace delimiter.
/\s+/g with replace(" ", ...) — collapse multiple spaces.
/^\s+|\s+$/g — trim leading and trailing whitespace.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern
One whitespace
\s
Whitespace run
\s+
Split into words
split(/\s+/)
Collapse spaces
/\s+/g → single space
Trim edges
/^\s+|\s+$/g
Opposite
\S
Compare
📋 \s vs space vs \S
Three related patterns for whitespace handling.
Whitespace
\s
All types
Literal
Space only
Non-space
\S
Inverse
Many gaps
\s+
Quantifier
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover detection, splitting, normalization, trimming, and comparison with \S.
📚 Getting Started
Detect whether a string contains any whitespace character.
Example 1 — Test for Any Whitespace
Return true when a space, tab, or newline appears in the text.
^\s+ removes leading whitespace; \s+$ removes trailing. For everyday trimming, String.trim() is simpler, but the regex shows how \s works with anchors.
Example 5 — Count Lines in a Text Block
Split on newline whitespace to count non-empty lines.
The \s whitespace metacharacter is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \s
Supported in every browser and Node.js version. \s matches standard ASCII whitespace characters.
99%Universal syntax
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 everywhere. Use \s as the standard shorthand for matching whitespace.
Wrap Up
Conclusion
The \s metacharacter is the fastest way to match whitespace in JavaScript regular expressions. It covers spaces, tabs, newlines, and related separators in one shorthand.
Use split(/\s+/) to tokenize words, replace(/\s+/g, " ") to normalize spacing, and remember the inverse \S for non-whitespace matching. Next, learn \t for tab characters specifically.