Character classes
A character class [abc] matches one character from the set; ranges like [a-z0-9] cover spans, and a leading ^ negates the set. Shorthands: \d is a digit [0-9], \w a word character [A-Za-z0-9_], \s whitespace, and \D \W \S match the opposite. The dot matches any character except a newline.
Syntax
[set] [^set] \d \w \s \D \W \S .| Part | What it is |
|---|---|
\d | a digit, 0 to 9 |
\w | a word character: letter, digit or underscore |
\s | a whitespace character: space, tab, newline and others |
\D \W \S | anything that is not a digit, word character or whitespace |
. | any character except a line break (any character with the s flag) |
[^...] | any one character not listed |
Examples
"gray grey groy".match(/gr[ae]y/g)Result: ["gray", "grey"]
"A1 b2 C3".match(/[A-Z]\d/g)Result: ["A1", "C3"]
"x-1, y+2".match(/[+-]\d/g)Result: ["-1", "+2"]
A hyphen is literal when it is the last character of the class.
"file_01.txt".match(/\w+/g)Result: ["file_01", "txt"]
The dot is not a word character, so it splits the matches.
Common mistakes
- Writing [a-Z]: an invalid range, because Z sorts before a. Use [a-zA-Z].
- Thinking [abc] matches the string abc; it matches one character. Use abc or (abc) for the sequence.
- Placing a hyphen in the middle, as in [a-z0-9-_], which creates an accidental range; put it first, last, or escape it.
- Escaping the dot inside a class as [\.]: harmless, but [.] already means a literal dot, and most other characters are literal inside brackets too.
Practice Character classes
These drills check your answer on live data, free, no account needed.
- Character class: gray or greyThe whole line must match. Match both spellings, gray and grey, and nothing else.
- Ranges: a letter and two digitsThe whole line must match. Match one lowercase letter followed by exactly two digits, like a12.
- Negated class: no vowelsThe whole line must match. Match lines that contain none of the lowercase vowels a, e, i, o or u. Any other characters are fine, including uppercase letters, digits and spaces.
Questions
What is the difference between [0-9] and \d?
In JavaScript they are identical. In Python 3, .NET and Java, \d also matches digits from other scripts, so [0-9] is the safe choice for ASCII only.
How do I match a literal ] or ^ inside a character class?
Escape them: [\]^] matches a closing bracket or a caret. A caret is literal anywhere except the first position, and a hyphen is literal first or last.
Does the dot match a newline?
Not by default. Add the s flag (/a.b/s) or use [\s\S] to match any character including line breaks.
Related
- Negated character classHow [^...] matches any character except the listed ones, stripping non-digits, grabbing quoted text, splitting on a delimiter, and how it differs from (?!).
- Ranges in character classesHow character ranges work by code point, how to combine them, the invalid [a-Z] and surprising [A-z], and how to express numeric ranges with alternation.
- QuantifiersWhat * + ? {n} {n,} and {n,m} mean, how greedy matching works, how to repeat a group instead of a single character, and the silent {2, 3} mistake.
- Dot and newlineWhy . does not match line breaks by default, the s flag in JavaScript and re.DOTALL in Python, the portable [\s\S] trick, and how s differs from m.