The \w metacharacter matches any single word character: ASCII letters A–Z and a–z, digits 0–9, and underscore (_). It is shorthand for [A-Za-z0-9_] and appears in patterns that extract tokens, validate identifiers, or split text on punctuation and spaces.
01
Word
\w
02
Shorthand
[A-Za-z0-9_]
03
One char
Letter, digit, or _
04
Quantifiers
\w+
05
Opposite
\W
06
Validate
^\w+$
Fundamentals
Introduction
Variable names, usernames, file slugs, and API keys often follow a predictable shape: letters, numbers, and sometimes underscores. The \w metacharacter gives you a compact way to target those characters without listing every letter and digit manually.
One \w matches exactly one word character. To match a full token like user_42, combine \w with quantifiers like + (one or more) or {3,20} (between 3 and 20 characters).
Concept
Understanding the \w Metacharacter
\w is a predefined character class escape. In JavaScript it matches ASCII letters A–Z and a–z, digits 0 through 9, and underscore (_).
Pair \w with anchors (^, $), quantifiers (+, {n,m}), and \b word boundaries when you need whole tokens instead of partial matches inside longer strings.
Foundation
📝 Syntax
JavaScript
\w // one word char (A–Z, a–z, 0–9, _)
\w+ // one or more word chars (a token)
\w{3,20} // between 3 and 20 word chars
\w* // zero or more word chars
\W // one non-word character
Common patterns
/\w+/ — match a run of word characters (a token).
/^\w+$/ — validate an alphanumeric identifier.
/\b\w+\b/g — find standalone words in a sentence.
/\W+/ — match separators (spaces, punctuation) between words.
Cheat Sheet
⚡ Quick Reference
Goal
Pattern
One word char
\w
Word token
\w+
Length limit
\w{3,20}
Word chars only
/^\w+$/
Non-word char
\W
Same as \w
[A-Za-z0-9_]
Compare
📋 \w vs [A-Za-z0-9_] vs \W
Three related patterns for word and non-word characters.
Word
\w
Shorthand
Class
[A-Za-z0-9_]
Equivalent
Not word
\W
Opposite
Many chars
\w+
Quantifier
Hands-On
Examples Gallery
Open DevTools Console (F12) or use Try-it links. Examples cover detection, extraction, validation, masking, and splitting with non-word characters.
📚 Getting Started
Detect whether a string contains any word character.
Example 1 — Test for Any Word Character
Return true when at least one letter, digit, or underscore appears in the text.
\w+ greedily collects consecutive word characters. Spaces and punctuation break the match, so hello_world stays one token because underscore counts as a word character.
Example 3 — Validate a Username Token
Ensure input contains only word characters within a sensible length.
The \w word metacharacter is part of core JavaScript regular expression syntax.
✓ Baseline · ES3+
RegExp \w
Supported in every browser and Node.js version. \w and [A-Za-z0-9_] behave identically for ASCII 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 letters, digits, and underscore.
Wrap Up
Conclusion
The \w metacharacter is the fastest way to match word characters in JavaScript regular expressions. It covers letters, digits, and underscore, pairs naturally with quantifiers and anchors, and has a clear inverse in \W.
Use \w+ to extract tokens, /^\w+$/ to validate identifiers, and remember that it matches ASCII word characters only by default. Next, learn the \xHH hex escape.
Expect \w alone to match full multi-character tokens
Assume accented or Unicode letters match without the u flag
Forget the global flag when replacing all word characters
Use \w when you only need digits—prefer \d
Mix \w and manual [A-Za-z0-9_] randomly in one project
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about RegExp \w
Match letters, digits, and underscore in JavaScript patterns.
5
Core concepts
🔢01
Word char
\w
Syntax
📝02
= [A-Za-z0-9_]
Same set.
Compare
📈03
\w+
Tokens.
Quantifier
🔒04
^\w+$
ID rule.
Validate
🚫05
\W
Not word.
Inverse
❓ Frequently Asked Questions
\w matches any single word character: ASCII letters A–Z and a–z, digits 0–9, and underscore (_). It is shorthand for [A-Za-z0-9_] in JavaScript.
Yes. In standard JavaScript regex without the u flag, \w and [A-Za-z0-9_] match the same set of characters.
By itself, \w matches exactly one word character. Add a quantifier like + (\w+) or {3,20} to match longer tokens such as usernames or identifiers.
\W is the opposite of \w — it matches any single character that is NOT a word character. It is equivalent to [^A-Za-z0-9_] in JavaScript.
By default, \w matches ASCII letters only. For full Unicode letters, use the u flag with \p{L} or \p{Word} property escapes on modern engines.
The \w word-character shorthand is core regex syntax since ES3. It works in every browser and Node.js version.
Did you know?
JavaScript also provides \d (digit), \s (whitespace), and \W (non-word). Together with \w, these four shorthand classes cover most basic parsing tasks without writing full character classes.