JavaScript Regex Character Classes
Learn JavaScript regex character classes: the shorthands \d \w \s, their negations \D \W \S, the dot, the s (dotAll) flag, and custom classes.
Character Classes in JavaScript
A character class is a piece of a regular expression that matches a single character belonging to a particular set — for example "any digit" or "any whitespace character". Instead of spelling out every possible character, you describe the kind of character you want, and the regex engine does the matching.
This page covers the built-in shorthand classes (\d, \w, \s), their negations (\D, \W, \S), the special dot . and the s (dotAll) flag, and how to build your own classes with square brackets [...], including ranges and escaping.
Every class matches exactly one character. To match several, you combine a class with a quantifier such as + or {2,4}.
The shorthand classes: \d \w \s
JavaScript provides three shorthand classes for the most common groups of characters.
| Class | Matches | Equivalent |
|---|---|---|
\d | a digit | [0-9] |
\w | a word character: a letter, digit, or underscore | [A-Za-z0-9_] |
\s | a whitespace character: space, tab \t, newline \n, etc. | — |
A single shorthand matches one character. Add a quantifier to match a run of them — \d+ means "one or more digits":
The negated classes: \D \W \S
Each shorthand has an uppercase partner that matches the opposite set — any character that is not in the original class.
| Class | Matches |
|---|---|
\D | any character that is not a digit |
\W | any character that is not a word character |
\S | any character that is not whitespace |
A handy trick: \D (or [^\d]) lets you strip everything except digits, which is common when cleaning up user input like phone numbers.
The dot . and the s (dotAll) flag
The dot . is a special character class that matches any single character except a line break (\n, \r, and a couple of Unicode line separators).
When you do want the dot to match newlines as well, add the s flag (short for "dotAll"):
See patterns and flags for the full list of regex flags such as g, i, and s.
Custom character classes: [...]
When the shorthands don't fit, build your own class by listing characters inside square brackets. [abc] matches a single a, b, or c.
Ranges
A hyphen - between two characters creates a range: [a-z] matches any lowercase letter, [0-9] any digit. You can combine several ranges and literal characters in one class. (For a deeper look, see sets and ranges.)
Negated custom classes [^...]
A caret ^ as the first character inside the brackets negates the class: [^a-z] matches any character that is not a lowercase letter.
Mixing shorthands and characters
Shorthand classes work inside custom classes too. [\w.] matches a word character or a literal dot — useful for tokens like version strings or filenames.
Escaping inside character classes
Inside [...], most regex metacharacters lose their special meaning, so you usually do not need to escape them. For example [.] matches a literal dot — there's no need for [\.].
A few characters still need care:
- Hyphen
-— means a range between two characters. To match a literal hyphen, put it first, last, or escape it:[-+],[+-], or[+\-]. - Caret
^— negates the class only when it's first. Anywhere else ([a^]) it's a literal^. - Closing bracket
]and backslash\— always escape these:[\]],[\\].
A misplaced hyphen can silently create an unintended range. [a-z] is a range, but [z-a] throws a syntax error, and [%-/] matches every character whose code point falls between % and /. When in doubt, escape the hyphen or move it to the edge of the class.
Unicode and the u flag
By default \w, \d, and \s only recognize ASCII. With the u flag you unlock Unicode-aware matching and property escapes like \p{Letter}, which match characters from any language. See the unicode flag u and class \p for the details.
Conclusion
Character classes let you describe what kind of character to match instead of listing every option:
\d \w \smatch digits, word characters, and whitespace;\D \W \Smatch their opposites.- The dot
.matches any character except a line break — unless you add thes(dotAll) flag. - Square brackets
[...]build custom sets; add ranges with-, negate with a leading^, and remember that most metacharacters are literal inside a class.
Pair these classes with quantifiers to match repeated characters and build powerful, readable patterns.