The \W metacharacter matches any single character that is not a word character. Word characters are letters, digits, and underscore (_). Everything else—spaces, hyphens, punctuation, and symbols—matches \W.
01
Non-word
\W
02
Shorthand
[^\w]
03
Inverse
Opposite of \w
04
Quantifiers
\W+
05
Strip
replace(/\W/g, "")
06
Split
split(/\W+/)
Fundamentals
Introduction
When parsing user input, cleaning data, or splitting sentences, you often need to target punctuation and separators rather than letters and numbers. The \W metacharacter matches any character that is not part of the word-character set.
One \W matches exactly one non-word character. To match a run of punctuation or whitespace separators, combine \W with + or use the global flag with replace().
Concept
Understanding the \W Metacharacter
\W is a negated predefined character class escape. In JavaScript a word character (\w) is equivalent to [A-Za-z0-9_]. Therefore \W matches everything except letters, digits, and underscore.
JavaScript
/\W/.test("hello"); // false (all word chars)
/\W/.test("hello!"); // true ("!" matches)
/\W/.test("user_name"); // false (_ is a word char)
/\W/.test("user-name"); // true ("-" matches)
"hello, world!".match(/\W+/g); // [", ", "!"]
The lowercase form \w matches word characters only. Remember: underscore counts as a word character, but hyphen does not.
💡
Beginner Tip
To create a simple slug from a title, write title.replace(/\W+/g, "-"). Non-word runs become hyphens, though you may still want to trim and lowercase separately.
Usage
How to Use \W in JavaScript
Use \W in detection, extraction, cleaning, splitting, and validation workflows:
Pair \W with anchors, quantifiers, and methods like test(), match(), replace(), and split().
Foundation
📝 Syntax
JavaScript
\W // one non-word character
\W+ // one or more non-word chars
\W* // zero or more non-word chars
/\W/g // global: every non-word char
\w // one word char (inverse of \W)
Common patterns
/\W+/g — match runs of punctuation or separators.
/\W/g + replace("", ...) — remove all non-word characters.
split(/\W+/) — break text on non-word boundaries.
/^\w+$/ — validate word characters only (no \W allowed).
Cheat Sheet
⚡ Quick Reference
Goal
Pattern
One non-word char
\W
Punctuation run
\W+
Remove symbols
/\W/g + replace("", ...)
Split on separators
split(/\W+/)
Same as \W
[^\w]
Opposite
\w
Compare
📋 \W vs [^\w] vs \w
Three related patterns for non-word and word characters.
Non-word
\W
Shorthand
Class
[^\w]
Equivalent
Word char
\w
Inverse
Word set
[A-Za-z0-9_]
Same as \w
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover detection, extraction, symbol removal, splitting, and alphanumeric validation.
📚 Getting Started
Detect whether a string contains any non-word character.
Example 1 — Test for Any Non-Word Character
Return true when punctuation, spaces, or symbols appear in the text.
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
The \W non-word metacharacter is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \W
Supported in every browser and Node.js version. \W and [^\w] behave identically for standard word 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
\WExcellent
Bottom line: Safe everywhere. Use \W as the standard shorthand for matching non-word characters.
Wrap Up
Conclusion
The \W metacharacter is the fastest way to match non-word characters in JavaScript regular expressions. It targets spaces, punctuation, hyphens, and symbols—everything except letters, digits, and underscore.
Use \W+ to find separator runs, /\W/g with replace() to strip symbols, and remember that underscore is a word character. Next, learn \B for non-word-boundary matching.
\W matches any single character that is NOT a word character. Word characters are letters 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.
Did you know?
JavaScript’s three uppercase inverse shorthands—\D, \S, and \W—complete the lowercase trio \d, \s, and \w. Uppercase always means “everything except” the lowercase form.