Regex Character Classes — Complete Guide to \d, \w, \s and Custom Sets
Learn all regex character classes including \d for digits, \w for word characters, \s for whitespace, custom sets [abc], ranges [a-z], and negated classes [^abc].
Detailed Explanation
Understanding Regex Character Classes
Character classes are one of the most fundamental building blocks in regular expressions. They let you match a single character from a defined set, giving you precise control over what your pattern accepts.
Built-in Shorthand Classes
JavaScript provides several shorthand character classes that cover the most common matching needs:
| Shorthand | Equivalent | Matches |
|---|---|---|
\d |
[0-9] |
Any digit |
\D |
[^0-9] |
Any non-digit |
\w |
[a-zA-Z0-9_] |
Any word character |
\W |
[^a-zA-Z0-9_] |
Any non-word character |
\s |
[ \t\n\r\f\v] |
Any whitespace |
\S |
[^ \t\n\r\f\v] |
Any non-whitespace |
Custom Character Sets
Square brackets define custom sets: [aeiou] matches any vowel. You can combine ranges and individual characters: [a-zA-Z0-9_-] matches letters, digits, underscores, and hyphens.
Negated Character Classes
Placing ^ at the start of a character class negates it: [^0-9] matches any character that is NOT a digit. This is different from ^ as an anchor outside brackets.
The Dot Metacharacter
The dot . matches any single character except a newline. With the s (dotall) flag, it matches newlines too. When you need a literal dot, escape it: \.
Practical Tips
- Use shorthands (
\d,\w) for readability when they match your intent exactly - Use custom sets when you need to match a specific subset (e.g.,
[a-f0-9]for hex digits) - Remember that inside
[], most metacharacters lose their special meaning except],\,^, and-
Use Case
You are parsing user input and need to validate that a field contains only specific characters, such as digits for a phone number, alphanumeric characters for a username, or a specific set of allowed symbols.