Character Classes
A character class — square brackets around a set of characters — matches exactly one character from that set. [aeiou] matches one vowel; gr[ae]y matches both spellings of a colour.
/gr[ae]y/g
grey, gray, groy, graey2 matches
#1 "grey" at index 0
#2 "gray" at index 6
graey did not match: the class stands for one character, so gr[ae]y can never match five letters.
Ranges
Inside a class, a hyphen between two characters means "everything from … to …" (by character code):
| Class | Matches one… |
|---|---|
[0-9] |
digit |
[a-z] |
lowercase ASCII letter |
[A-Z] |
uppercase ASCII letter |
[a-zA-Z] |
ASCII letter of either case |
[0-9a-fA-F] |
hexadecimal digit |
[a-z0-9_-] |
lowercase letter, digit, underscore or hyphen |
/#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]/g
Colours: #fff, #0A3, #xyz, #12g2 matches
#1 "#fff" at index 9
#2 "#0A3" at index 15
Negated classes
A caret ^ as the first character inside the brackets inverts the class: [^0-9] matches any single character that is not a digit — including spaces, punctuation and newlines.
/[^aeiou ]/g
a bee1 match
#1 "b" at index 2
Negated classes are great for "everything up to a delimiter". "[^"]*" reads as: a quote, any number of non-quote characters, a closing quote.
/"[^"]*"/g
name="Lan" city="Hue" empty=""3 matches
#1 "\"Lan\"" at index 5
#2 "\"Hue\"" at index 16
#3 "\"\"" at index 28
Special characters inside a class
Inside brackets most metacharacters lose their power. [.*+?] simply matches a dot, star, plus or question mark. Only a few characters still need attention:
| Character | Inside [...] |
How to make it literal |
|---|---|---|
] |
closes the class | \] |
\ |
escape | \\ |
^ |
negates, only at the start | put it elsewhere, or \^ |
- |
range, only between two characters | put it first or last, or \- |
/[+\-*\/^]/g
3+4-2*8/2^25 matches
#1 "+" at index 1
#2 "-" at index 3
#3 "*" at index 5
#4 "/" at index 7
#5 "^" at index 9
Classes plus repetition
A class matches one character, but you can repeat it with a quantifier (full details in the Quantifiers lesson). [A-Z][a-z]+ means "a capital letter followed by one or more lowercase letters":
/[A-Z][a-z]+/g
Visit Hanoi, Da Nang and HCMC in May.5 matches
#1 "Visit" at index 0
#2 "Hanoi" at index 6
#3 "Da" at index 13
#4 "Nang" at index 16
#5 "May" at index 33
HCMC is skipped: after H comes C, not a lowercase letter.
Note:
[a-z]covers only the 26 ASCII letters. Words like café or Đà Nẵng contain letters outside that range — see the Unicode lesson for\p{L}, which matches letters from every script.
A common mistake
[cat|dog] does not mean "cat or dog". It is a class of the characters c a t | d o g, and it matches only one of them at a time. For whole alternatives use cat|dog (Alternation lesson).
/[cat|dog]/g
c|g3 matches
#1 "c" at index 0
#2 "|" at index 1
#3 "g" at index 2
Test yourself
What does [^0-9] match?
- A digit at the start of a line
- Any one character that is not a digit
- The characters ^, 0 and 9
- Zero or more digits
How many characters does [abc] match at a time?
- Exactly one
- One to three
- Exactly three
- Any number
Which class matches a literal hyphen or a digit?
[0-9-][0--9][-0-9-]+?[0\-9]only