JavaScript RegExp Non-Word Metacharacter (\W)

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

What Is \W?

\W matches any single character that is not a word character. Word characters are letters, digits, and underscore ([A-Za-z0-9_]). Spaces, hyphens, and punctuation all match \W.

Remember
\w  → letter, digit, or underscore
\W  → everything else (space, -, !, @, …)
\W+ → one or more non-word characters

/\W/.test("hello")      → false
/\W/.test("hello!")     → true
/\W/.test("user_name")  → false  (_ is \w)
/\W/.test("user-name")  → true   (- is \W)
UsePrefer
Detect punctuation/symbols/\W/
Strip symbolsstr.replace(/\W/g, "")
Split on separatorsstr.split(/\W+/)
Match word chars\w

How to Use \W

Use \W when you care about “not a word character” — detect it, strip it, or split on it.

  1. Write /\W/ to match one non-word character.
  2. Add + for a run of separators (\W+).
  3. Use the g flag with replace or match to process every match.
  4. Remember: underscore is a word character — it does not match \W.
PieceRequired?Role
\W in the patternYesMatch one non-word character
QuantifierOptionalRepeat (\W+, \W*, \W{2})
g flagOptionalFind or replace every non-word character
Anchors / inverseOptional/^\w+$/ or !/\W/.test(str) for word-only text

Pseudocode

Pseudocode
pattern has "\W"
read next character ch from the string

if ch is letter, digit, or underscore:
    fail this path
else:
    accept ch as a non-word match

common actions:
  test(str)              → true if any symbol/space/punct exists
  replace(/\W/g, "")     → keep letters, digits, _
  split(/\W+/)           → tokens between separators

Cheat Sheet

Keep these facts handy while cleaning punctuation and symbols.

TopicDetail
MeaningNot [A-Za-z0-9_]
Equivalent[^\w]
Opposite\w = word character
UnderscoreMatches \w, not \W
Strip symbolsstr.replace(/\W/g, "")
Split tokensstr.split(/\W+/)
StandardES3+ (universal)

Live Preview

Run common \W operations on sample strings.

Browser demo of the RegExp \W non-word escape — same ideas as the examples below.

Result
text = "hello"
/\W/.test(text)
→ false

/* letters are word characters */

JavaScript Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Detect Any Non-Word Character

Return true when punctuation, spaces, or symbols appear — and see why underscore does not.

JavaScript
console.log(/\W/.test("hello"));      // false
console.log(/\W/.test("hello!"));     // true
console.log(/\W/.test("user_name"));  // false (_ is word char)
console.log(/\W/.test("user-name"));  // true
Try It Yourself

How It Works

  1. Letters only. "hello" is all word characters → false.
  2. Punctuation matches. "!" is non-word → true.
  3. Underscore is special. _ counts as \w, so "user_name" has no \W.
  4. Hyphen is different. "-" is non-word, so "user-name" matches.

Example 2 — Strip All Non-Word Characters

Remove punctuation and symbols, keeping letters, digits, and underscores.

JavaScript
const title = "Hello, World!";
console.log(title.replace(/\W/g, "")); // "HelloWorld"

const email = "[email protected]";
console.log(email.replace(/\W/g, "")); // "useremailcom"
Try It Yourself

How It Works

  1. Global replace. /\W/g finds every non-word character.
  2. Delete matches. Replacing with "" removes commas, spaces, @, and dots.
  3. Word chars remain. Letters (and digits/_ if present) stay in place.
  4. Use carefully. Stripping email punctuation also removes @ and . — intentional for cleanup demos.

Example 3 — Split Text on Non-Word Boundaries

Break a comma-, semicolon-, or space-separated list into tokens.

JavaScript
const csv = "one,two;three four";
console.log(csv.split(/\W+/));
// ["one", "two", "three", "four"]

const tags = "js,regex,pattern";
console.log(tags.split(/\W+/));
// ["js", "regex", "pattern"]
Try It Yourself

How It Works

  1. Use \W+ as a delimiter. One or more non-word characters become cut points.
  2. Mixed separators work. Commas, semicolons, and spaces all split the first string.
  3. Tokens are word runs. Each piece is letters/digits/_ between separators.
  4. Simple lists. "js,regex,pattern" splits cleanly on each comma.

Edge Cases & Pitfalls

Check these before relying on \W in real projects.

trap

Underscore is not \W

_ is a word character. "a_b" has no \W match; "a-b" does.

ascii

Default \w/\W are ASCII-oriented

Without Unicode flags/properties, “word” means [A-Za-z0-9_]. Accented letters may match \W in default mode.

escape

new RegExp needs double escaping

Write new RegExp("\\W"). In a string pattern argument you need two backslashes.

split

Leading/trailing separators can yield empty parts

",a,".split(/\W+/) may include empty strings. Filter with .filter(Boolean) when needed.

Browser & Runtime Support

The \W non-word escape is core regex syntax (ES3+) and works everywhere JavaScript runs.

✓ Baseline · ES3+

RegExp \W

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
\W Excellent

Bottom line: Safe to use in any JavaScript environment. Pair with \w when you need both sides of the word-character check.

Performance Notes

ApproachCostBest for
/\W/.test(str)O(n)Quick “has symbol?” checks
str.replace(/\W/g, "")O(n)Stripping punctuation
str.split(/\W+/)O(n)Tokenizing on separators

Character-class style matches are cheap. Prefer \w when you want word characters directly, and \W when stripping or splitting on everything else is clearer.

Key Takeaways

  • Role: \W matches one character that is not a word character.
  • Opposite of \w: letters/digits/_ vs everything else.
  • Cleanup: replace(/\W/g, "") strips punctuation and spaces.
  • Split: split(/\W+/) tokenizes on commas, spaces, and symbols.

One line: \W means “not a word character” — detect symbols, strip them, or split on them (and remember _ is \w).

Frequently Asked Questions

\W matches any single character that is NOT a word character. Word characters are letters A–Z/a–z, digits 0–9, and underscore (_). Spaces, hyphens, and punctuation all satisfy \W.
\w matches word characters (letters, digits, underscore). \W is the uppercase inverse and matches everything except those characters—spaces, symbols, and punctuation.
Yes. In standard JavaScript regex, \W and [^\w] are equivalent—they both match one non-word character.
No. Underscore (_) is a word character and matches \w, not \W. Hyphens, spaces, and @ symbols do match \W.
Use replace with the global flag: str.replace(/\W/g, ""). Every non-word character is removed, leaving letters, digits, and underscores.
The \W non-word shorthand is core regex syntax since ES3. It works in every browser and Node.js version.

Next: \B non-boundary

Learn how the \B metacharacter matches positions that are not word boundaries.

\B 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