W3docs

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.

ClassMatchesEquivalent
\da digit[0-9]
\wa word character: a letter, digit, or underscore[A-Za-z0-9_]
\sa whitespace character: space, tab \t, newline \n, etc.
javascript— editable

A single shorthand matches one character. Add a quantifier to match a run of them — \d+ means "one or more digits":

javascript— editable

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.

ClassMatches
\Dany character that is not a digit
\Wany character that is not a word character
\Sany character that is not whitespace
javascript— editable

A handy trick: \D (or [^\d]) lets you strip everything except digits, which is common when cleaning up user input like phone numbers.

javascript— editable

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).

javascript— editable

When you do want the dot to match newlines as well, add the s flag (short for "dotAll"):

javascript— editable

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.

javascript— editable

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.)

javascript— editable

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.

javascript— editable

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.

javascript— editable

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: [\]], [\\].
javascript— editable
Warning

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 \s match digits, word characters, and whitespace; \D \W \S match their opposites.
  • The dot . matches any character except a line break — unless you add the s (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.

Practice

Practice
Which of the following are valid character classes in JavaScript?
Which of the following are valid character classes in JavaScript?
Was this page helpful?