JavaScript RegExp Word Character Metacharacter (\w)

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

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_].

Remember
\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"]
UsePrefer
Detect a word char/\w/
Extract tokensstr.match(/\w+/g)
Validate identifier/^\w+$/
Opposite class\W

How to Use \w

Match one character, or add + when a whole token matters.

  1. Start with /\w/ to ask “is there any word character?”
  2. Add + for runs: /\w+/ matches identifiers and words.
  3. Use match(/\w+/g) to extract every token.
  4. Use /^\w+$/ to require the whole string to be word characters only.
PieceRequired?Role
\w in the patternYesMatch one word character
+ / * / {n,m}OptionalMatch longer tokens
^ … $OptionalValidate the entire string
Inverse—\W matches non-word characters

Pseudocode

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

Cheat Sheet

Keep these patterns handy for identifiers and tokens.

TopicDetail
Matches[A-Za-z0-9_]
One character\w
Token / word\w+
Extract allmatch(/\w+/g)
Validate/^\w+$/
Opposite\W
StandardES3+ (universal)

Live Preview

Run common \w operations on sample strings with letters, digits, and symbols.

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

Result
text = "!!!"
/\w/.test(text)
→ false

/* punctuation is not a word character */

JavaScript Examples

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

Example 1 — Detect a Word Character

Return true when a letter, digit, or underscore appears.

JavaScript
console.log(/\w/.test("!!!")); // false
console.log(/\w/.test("a"));   // true
console.log(/\w/.test("_"));   // true
console.log(/\w/.test("9"));   // true
Try It Yourself

How It Works

  1. Punctuation fails. "!" is not a word character → false.
  2. Letters pass. "a" is in A–Z / a–z.
  3. Underscore counts. "_" is treated as a word character.
  4. Digits count too. "9" matches \w.

Example 2 — Extract Word Tokens

Pull identifiers out of messy text with \w+.

JavaScript
const text = "hi_42! go-now";

console.log(text.match(/\w+/g));
// ["hi_42", "go", "now"]
Try It Yourself

How It Works

  1. Use \w+. One or more word characters form a token.
  2. Global match. The g flag finds every run.
  3. Punctuation splits. ! and - break tokens apart.
  4. 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.

JavaScript
console.log(/^\w+$/.test("user_1")); // true
console.log(/^\w+$/.test("user-1")); // false
console.log(/^\w+$/.test(""));       // false
Try It Yourself

How It Works

  1. Anchor the string. ^ and $ mean every character must match.
  2. Require one or more. \w+ rejects the empty string.
  3. Underscore is fine. "user_1" passes.
  4. 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 default

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

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.

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 the non-word side.

Performance Notes

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

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

\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 Unicode property escapes, \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. Spaces, hyphens, and punctuation all satisfy \W.
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.
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

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.

6 people found this page helpful