JavaScript RegExp Non-Whitespace Metacharacter (\S)

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is \S?

\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"]
UsePrefer
Detect visible content/\S/
Tokenize wordsstr.match(/\S+/g)
No spaces allowed/^\S+$/
Match whitespace\s

How to Use \S

Use \S when you care about “visible” characters — detect them, tokenize them, or require them.

  1. Write /\S/ to match one non-whitespace character.
  2. Add + for a run of visible characters (\S+) — useful for tokens/words.
  3. Use the g flag with match to collect every token.
  4. Prefer \s when you want spaces; prefer \S when you want everything else.
PieceRequired?Role
\S in the patternYesMatch one non-whitespace character
QuantifierOptionalRepeat (\S+, \S*, \S{2})
g flagOptionalFind every non-whitespace run
Anchors ^ $OptionalRequire 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

Cheat Sheet

Keep these facts handy while tokenizing and validating text.

TopicDetail
MeaningAny character except whitespace
Equivalent[^\s]
Opposite\s = whitespace
One or more\S+ (typical “word/token”)
Tokenizestr.match(/\S+/g)
No spaces/^\S+$/
StandardES3+ (universal)

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 */

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.

JavaScript
console.log(/\S/.test("   "));     // false
console.log(/\S/.test("hello"));   // true
console.log(/\S/.test("  hi  "));  // true
Try It Yourself

How It Works

  1. Whitespace only. " " has no visible character, so /\S/ is false.
  2. Letters count. "hello" matches immediately → true.
  3. Padded text still matches. Leading/trailing spaces do not hide the "hi" in the middle.
  4. One char is enough. test stops at the first non-whitespace.

Example 2 — Tokenize Words With \S+

Extract word-like tokens from text with irregular spacing.

JavaScript
const messy = "  hello   world\tfoo  ";
console.log(messy.match(/\S+/g));
// ["hello", "world", "foo"]

const line = "Price: $19  today";
console.log(line.match(/\S+/g));
// ["Price:", "$19", "today"]
Try It Yourself

How It Works

  1. Quantify. \S+ matches one or more consecutive non-whitespace characters.
  2. Whitespace is the boundary. Spaces and tabs split the string into separate tokens.
  3. Punctuation stays attached. "Price:" and "$19" are single tokens because they contain no spaces.
  4. Global match. The g flag returns every token, not only the first.

Example 3 — Validate Input With No Spaces

Ensure a username or slug contains no whitespace characters.

JavaScript
const noSpaces = /^\S+$/;

console.log(noSpaces.test("username"));   // true
console.log(noSpaces.test("user_name"));  // true
console.log(noSpaces.test("user name"));  // false
console.log(noSpaces.test("   "));        // false
Try It Yourself

How It Works

  1. Anchor the whole string. ^ and $ mean every character must match.
  2. Require visible chars. \S+ needs one or more non-whitespace characters.
  3. Underscores are fine. "user_name" has no whitespace → true.
  4. 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
\S Excellent

Bottom line: Safe to use in any JavaScript environment. Pair with \s when you need both sides of the whitespace check.

Performance Notes

ApproachCostBest 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.

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.

Next: \W non-word

Learn how the \W metacharacter matches any character that is not a word character.

\W tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful