JS RegExp Classes
In this page:
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
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
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
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
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 [ ]
console.log(/[aeiou]/.test("sky")); // false, no vowels
console.log(/[a-z]/.test("ABC1")); // false, no lowercase letters
- 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.
- 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.
- Confusing \s (any whitespace: space, tab, newline) with a literal space character in a pattern meant to match tabs or newlines too.
- \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.
All standard regex character classes are supported in every modern browser and have been part of JavaScript regex since its earliest implementation.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML