\S matches any single character that is not whitespace. It is the opposite of \s and equals [^\s] in ordinary JavaScript regex.
Remember
\s → one whitespace (space, tab, newline, …)
\S → one non-whitespace (everything else)
\S+ → one or more non-whitespace (a “token”)
/\S/.test(" ") → false
/\S/.test(" hi ") → true
"a b".match(/\S+/g) → ["a", "b"]
Use \S when you care about “visible” characters — detect them, tokenize them, or require them.
Write /\S/ to match one non-whitespace character.
Add + for a run of visible characters (\S+) — useful for tokens/words.
Use the g flag with match to collect every token.
Prefer \s when you want spaces; prefer \S when you want everything else.
Piece
Required?
Role
\S in the pattern
Yes
Match one non-whitespace character
Quantifier
Optional
Repeat (\S+, \S*, \S{2})
g flag
Optional
Find every non-whitespace run
Anchors ^$
Optional
Require the whole string to be non-whitespace
Pseudocode
Pseudocode
pattern has "\S"
read next character ch from the string
if ch is whitespace (space, tab, \n, \r, \f, …):
fail this path
else:
accept ch as a non-whitespace match
common actions:
test(str) → true if any visible char exists
match(/\S+/g) → tokens between whitespace
/^\S+$/.test(str) → no whitespace anywhere
Reference
Cheat Sheet
Keep these facts handy while tokenizing and validating text.
Topic
Detail
Meaning
Any character except whitespace
Equivalent
[^\s]
Opposite
\s = whitespace
One or more
\S+ (typical “word/token”)
Tokenize
str.match(/\S+/g)
No spaces
/^\S+$/
Standard
ES3+ (universal)
Try it
Live Preview
Run common \S operations on sample strings.
Browser demo of the RegExp \S non-whitespace escape — same ideas as the examples below.
Result
text = " "
/\S/.test(text)
→ false
/* only whitespace — no visible character */
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Detect Any Non-Whitespace
Return true when the string is not blank or whitespace-only.
Anchor the whole string.^ and $ mean every character must match.
Require visible chars.\S+ needs one or more non-whitespace characters.
Underscores are fine."user_name" has no whitespace → true.
Spaces fail. A middle space or a blank string fails the pattern.
Edge Cases & Pitfalls
Check these before relying on \S in real projects.
token
\S+ is not “letters only”
Punctuation stays attached: "hello," is one token. For letters only, prefer [a-zA-Z]+ or \w+.
empty
\S needs a character
/\S/.test("") is false. Use /^\S*$/ if the empty string should pass a “no spaces” check.
escape
new RegExp needs double escaping
Write new RegExp("\\S"). In a string pattern argument you need two backslashes.
scope
Whitespace includes more than space
\s / \S also treat tab, newline, form feed, and carriage return as whitespace — not only the space bar.
Compatibility
Browser & Runtime Support
The \S non-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 both sides of the whitespace check.
Analysis
Performance Notes
Approach
Cost
Best for
/\S/.test(str)
O(n)
Quick “has content?” checks
str.match(/\S+/g)
O(n)
Tokenizing messy text
/^\S+$/.test(str)
O(n)
No-whitespace validation
Character-class style matches are cheap. Prefer \s when you want whitespace itself, and \S when you want the visible parts.
Remember
Key Takeaways
Role:\S matches one character that is not whitespace.
Opposite of \s: whitespace vs everything else.
Tokenize:match(/\S+/g) splits on any whitespace.
Validate:/^\S+$/ rejects spaces, tabs, and newlines.
One line:\S means “not whitespace” — detect content, tokenize with \S+, or require no spaces with /^\S+$/.
Frequently Asked Questions
\S matches any single character that is NOT whitespace. Letters, digits, punctuation, and symbols all satisfy \S. Spaces, tabs, newlines, form feeds, and carriage returns do not.
\s matches whitespace. \S is the uppercase inverse and matches everything except whitespace.
Yes. In standard JavaScript regex, \S and [^\s] are equivalent—they both match one non-whitespace character.
Use match with a quantifier: str.match(/\S+/g). Each \S+ run is a contiguous non-whitespace token—ideal for splitting messy text into words.
By itself, \S matches exactly one non-whitespace character. Add + for one or more (\S+) or * for zero or more (\S*) to match longer tokens.
The \S non-whitespace shorthand is core regex syntax since ES3. It works in every browser and Node.js version.