TExcelTAD
Home › Regular Expressions › Character Classes

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.

regex · try it yourself
/gr[ae]y/g
grey, gray, groy, graey
2 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
regex · try it yourself
/#[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]/g
Colours: #fff, #0A3, #xyz, #12g
2 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.

regex · try it yourself
/[^aeiou ]/g
a bee
1 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.

regex · try it yourself
/"[^"]*"/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 \-
regex · try it yourself
/[+\-*\/^]/g
3+4-2*8/2^2
5 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":

regex · try it yourself
/[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).

regex · try it yourself
/[cat|dog]/g
c|g
3 matches
#1 "c" at index 0
#2 "|" at index 1
#3 "g" at index 2

Test yourself

What does [^0-9] match?

  1. A digit at the start of a line
  2. Any one character that is not a digit
  3. The characters ^, 0 and 9
  4. Zero or more digits

How many characters does [abc] match at a time?

  1. Exactly one
  2. One to three
  3. Exactly three
  4. Any number

Which class matches a literal hyphen or a digit?

  1. [0-9-]
  2. [0--9]
  3. [-0-9-]+?
  4. [0\-9] only