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.
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
Email Address Validation
Match and validate email addresses in the standard user@domain.tld format.
/[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+…/gUS Phone Number
Match US phone numbers in common formats: (555) 867-5309, 555-867-5309, 5558675309.
/\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d…/gInteger
Matches whole integers, including negative numbers.
/^-?\d+$/Decimal Number
Matches decimal numbers, including integers and negatives.
/^-?\d+(\.\d+)?$/