Concept

Character Classes: \d, \w, \s and Their Negations

Shorthand character classes match broad categories of characters. \d is a digit, \w is a word character, \s is whitespace. Uppercase negates.

The six shorthands

\d = digit [0-9]. \w = word character [A-Za-z0-9_]. \s = whitespace (space, tab, newline, etc.). Capitalize to negate: \D is any non-digit, \W is any non-word character, \S is any non-whitespace.

Examples

These shorthands compose with quantifiers and anchors to cover most practical validation cases cleanly.

All digits

Try this
/^\d+$/

Input

867-5309

Result

No match (hyphen isn't a digit)

Non-whitespace runs (word-like tokens)

Try this
/\S+/g

Input

one two\tthree

Result

Matches: one, two, three

Word characters only

Try this
/^\w+$/

Input

hello_world_42

Result

Match: hello_world_42

Unicode nuance

\w is ASCII-only by default in JavaScript. With the u flag you can use \p{L} for any Unicode letter or \p{N} for any Unicode digit. For CJK, Arabic, or Cyrillic text, the shorthands will miss characters you probably want to match.

When to reach for custom sets

If you need a restricted alphabet — lowercase letters and digits only, say — use a custom set like [a-z0-9] instead. See the custom character sets reference for patterns like [^0-9] and [a-fA-F].

Related patterns

All reference guidesOpen the RegexPro tester →