Home JavaScript Regular expressions \w Definition
What Is \w? \w matches one word character : ASCII letters (A–Z, a–z), digits (0–9), or underscore (_). It is shorthand for [A-Za-z0-9_].
\w → letter, digit, or underscore
\W → anything else (opposite)
/\w/.test("!") → false
/\w/.test("a") → true
/\w/.test("_") → true
"hi_42!".match(/\w+/g) → ["hi_42"] Use Prefer Detect a word char /\w/Extract tokens str.match(/\w+/g)Validate identifier /^\w+$/Opposite class \W
Approach
How to Use \w Match one character, or add + when a whole token matters.
Start with /\w/ to ask “is there any word character?” Add + for runs: /\w+/ matches identifiers and words. Use match(/\w+/g) to extract every token. Use /^\w+$/ to require the whole string to be word characters only. Piece Required? Role \w in the patternYes Match one word character + / * / {n,m}Optional Match longer tokens ^ … $Optional Validate the entire string Inverse — \W matches non-word characters
Pseudocode pattern has "\w"
read next character ch from the string
if ch is A–Z, a–z, 0–9, or "_":
accept match
else:
fail this path
common actions:
test(str) → true if any word char exists
match(/\w+/g) → list of word tokens
/^\w+$/.test(str) → whole string is alphanumeric/_ Reference
Cheat Sheet Keep these patterns handy for identifiers and tokens.
Topic Detail Matches [A-Za-z0-9_]One character \wToken / word \w+Extract all match(/\w+/g)Validate /^\w+$/Opposite \WStandard ES3+ (universal)
Try it
Live Preview Run common \w operations on sample strings with letters, digits, and symbols.
Code
JavaScript Examples Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
1. Detect 2. Tokens 3. Validate Example 1 — Detect a Word Character Return true when a letter, digit, or underscore appears.
console.log(/\w/.test("!!!")); // false
console.log(/\w/.test("a")); // true
console.log(/\w/.test("_")); // true
console.log(/\w/.test("9")); // true How It Works Punctuation fails. "!" is not a word character → false.Letters pass. "a" is in A–Z / a–z.Underscore counts. "_" is treated as a word character.Digits count too. "9" matches \w.Example 2 — Extract Word Tokens Pull identifiers out of messy text with \w+.
const text = "hi_42! go-now";
console.log(text.match(/\w+/g));
// ["hi_42", "go", "now"] How It Works Use \w+. One or more word characters form a token.Global match. The g flag finds every run.Punctuation splits. ! and - break tokens apart.Underscore stays inside. hi_42 is one token, not three.Example 3 — Validate an Identifier Allow only letters, digits, and underscore for the whole string.
console.log(/^\w+$/.test("user_1")); // true
console.log(/^\w+$/.test("user-1")); // false
console.log(/^\w+$/.test("")); // false How It Works Anchor the string. ^ and $ mean every character must match.Require one or more. \w+ rejects the empty string.Underscore is fine. "user_1" passes.Hyphen fails. "-" is not a word character → false. Edge Cases & Pitfalls Check these before relying on \w in real projects.
ascii \w is ASCII by defaultAccented letters like é and non-Latin scripts do not match. For Unicode letters, use property escapes with the u flag.
hyphen Hyphen is not a word character "user-name" fails /^\w+$/. Underscore is allowed; hyphen is not.
empty \w+ needs at least one character/^\w+$/.test("") is false. Use /^\w*$/ if the empty string should pass.
boundary Word characters ≠ word boundaries \w matches characters. For edges of words, use \b .
Compatibility
Browser & Runtime Support The \w word-character 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.
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 the non-word side.
Analysis
Performance Notes Approach Cost Best for /\w/.test(str)O(n) Quick “has word char?” checks str.match(/\w+/g)O(n) Tokenizing identifiers /^\w+$/.test(str)O(n) Username / ID validation
Character-class style matches are cheap. Prefer \W when you want punctuation and spaces, and \w when you want the tokens.
Remember
Key Takeaways Role: \w matches one letter, digit, or underscore.
Tokens: use \w+ to match identifiers and words.
Validate: /^\w+$/ requires the whole string to be word characters.
ASCII default: accented letters do not match plain \w.
One line: \w means letter/digit/underscore — detect it, tokenize with \w+, or require a clean identifier with /^\w+$/.
Frequently Asked Questions What does \w match in a JavaScript regular expression? \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.
Is \w the same as [A-Za-z0-9_]? Yes. In standard JavaScript regex without Unicode property escapes, \w and [A-Za-z0-9_] match the same set of characters.
Does \w match multiple characters at once? 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.
What is \W (uppercase W)? \W is the opposite of \w — it matches any single character that is NOT a word character. Spaces, hyphens, and punctuation all satisfy \W.
Does \w match Unicode letters like é or 字? By default, \w matches ASCII letters only. Accented letters and other scripts do not match unless you use Unicode property escapes with the u flag.
Which browsers support \w in RegExp? The \w word-character shorthand is core regex syntax since ES3. It works in every browser and Node.js version.
Next: \xHH hex escape Learn how two-digit hex escapes match byte values in patterns.
\xHH tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful
Helpful Share Copy link Suggestion