← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 20 of 26

JS RegExp Classes

Character classes are shorthand notations that match entire categories of characters at once -- \d for any digit, \w for any word character, \s for any whitespace -- and their uppercase opposites (\D, \W, \S) match everything NOT in that category, letting you build patterns without listing every individual character.

Digits with \d

\d matches any single digit character, 0 through 9 -- \d\d\d matches exactly three consecutive digits, and \d+ (combined with a quantifier) matches one or more digits in a row, the basis for matching numbers, phone digits, or zip codes.

Note: Combine \d with quantifiers (+ for one or more, {n} for exactly n) to match multi-digit numbers rather than just a single digit.

Warning: \d alone matches exactly one digit -- forgetting a quantifier when you meant to match a multi-digit number is a very common regex mistake.

Example: Digits with \d

javascript
console.log(/\d\d\d/.test("123")); // exactly 3 digits
console.log("Phone: 12345".match(/\d+/));

Word Characters with \w

\w matches any letter (upper or lower case), digit, or underscore -- effectively the set of characters typically valid in a variable name or simple identifier -- but notably does NOT include spaces, hyphens, or most punctuation.

Note: Remember \w excludes spaces and most punctuation -- if you need to match a full name with spaces, combine \w with an explicit space character or use \s alongside it.

Warning: Using \w+ to try to match a full sentence or phrase with spaces stops at the first space, since spaces are not part of the \w character class.

Example: Word Characters with \w

javascript
console.log(/^\w+$/.test("user_1"));  // true, letters/digits/underscore
console.log(/^\w+$/.test("user 1"));  // false, contains a space

Whitespace with \s

\s matches any whitespace character -- a regular space, a tab, or a newline -- useful for splitting text on any kind of whitespace or normalizing inconsistent spacing, rather than matching only a literal space character.

Note: Use \s+ (one or more whitespace characters) rather than a literal space when normalizing or splitting text that might contain tabs or multiple consecutive spaces.

Warning: A literal space character in a pattern only matches an actual space -- it will not match a tab or newline the way \s does, which can cause a pattern to miss text formatted with different whitespace.

Example: Whitespace with \s

javascript
console.log("a\tb  c".split(/\s/)); // splits on tab and spaces alike

Negated Classes: \D, \W, \S

The uppercase versions of the shorthand classes match the exact opposite of their lowercase counterparts -- \D matches any non-digit, \W matches any non-word-character, and \S matches any non-whitespace character.

Note: Use a negated class when it is more natural to describe what you want to exclude, rather than what you want to include.

Warning: A negated class like \D still matches whitespace and punctuation, not just letters -- it only excludes digits specifically, nothing more.

Example: Negated Classes: \D, \W, \S

javascript
console.log(/\D/.test("abc"));  // true, non-digit found
console.log(/\W/.test("a_b"));  // false, only word chars/underscore
console.log(/\S/.test("  "));   // false, only whitespace

Custom Character Classes with [ ]

Square brackets define a custom character class matching any single character from the specified set -- [aeiou] matches any single vowel, and [a-z] uses a range to match any single lowercase letter, letting you build precise character sets beyond the built-in shorthand classes.

Note: Use a range like [a-z] or [0-9] for a compact way to match any character within a contiguous range, rather than listing every individual character.

Warning: A custom character class without the i flag is case-sensitive by default -- [a-z] will not match an uppercase letter unless A-Z is added or the i flag is applied.

Example: Custom Character Classes with [ ]

javascript
console.log(/[aeiou]/.test("sky"));  // false, no vowels
console.log(/[a-z]/.test("ABC1"));   // false, no lowercase letters
Common Mistakes
  1. Forgetting that \w only matches letters, digits, and underscore -- it does not include spaces or punctuation, a common surprise when validating names or usernames with spaces.
  2. Using a custom character class like [a-z] without realizing it is case-sensitive by default, requiring either an explicit A-Z range too or the i flag.
  3. Confusing \s (any whitespace: space, tab, newline) with a literal space character in a pattern meant to match tabs or newlines too.
Chapter Summary
  • \d matches any digit (0-9), \w matches any word character (letters, digits, underscore), and \s matches any whitespace character.
  • The uppercase versions (\D, \W, \S) match the exact opposite -- any character NOT in that category.
  • Custom character classes, written in square brackets like [aeiou] or [a-z], match any single character from the specified set or range.
Browser Support

All standard regex character classes are supported in every modern browser and have been part of JavaScript regex since its earliest implementation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.