Character Classes
Match one character from a set with brackets and ranges, negate a set, and use the shorthand classes for digits, word characters and whitespace.
Welcome back!
Step 1 of 14Learn
One character, several choices
A product code in a warehouse starts with a letter A, B or C, followed by digits. How do you say "A or B or C" for one position? With a character class: a set of characters in square brackets. [ABC] matches exactly one character that is A, B or C.
Some examples:
gr[ae]ymatches "gray" and "grey."[0123456789]matches any one digit.[aeiou]matches one lowercase vowel.
The class always stands for a single character, however many options it lists. [ABC] against "AB" matches just the A; a second match would find the B.
Inside brackets, most metacharacters lose their special meaning. [.?*] matches a literal dot, question mark or asterisk, with no escaping needed. Only a few characters are special inside a class: ] (ends the class), \ (escape), ^ at the very start (negation) and - between two characters (a range). If you need a literal hyphen, put it first or last: [-+] matches a minus or plus sign.
Character classes are the workhorse of regex. Almost every real pattern uses them, because real text has positions where several characters are acceptable.
Step 2 of 14Diagram
Anatomy of a product code pattern
Tap a shape with to see more.
- [ABC][0-9]+
- [ABC]: one of A, B, C
A class always stands for exactly one character.
- [0-9]: one digit
A range inside a class.
- +: one or more
Repeats the class before it, so B7, A42 and C1009 all match.
Step 3 of 14Practice · Choose one
Which strings does the pattern b[aeiou]t match completely?
Step 4 of 14Learn
Ranges and negation
Listing every letter would be tedious, so classes support ranges with a hyphen:
[a-z]any lowercase letter[A-Z]any uppercase letter[0-9]any digit[A-Za-z0-9]any letter or digit[0-5]the digits 0 through 5
Ranges follow character codes, so [a-z] covers only the 26 unaccented English letters. It doesn't include "é" or "ñ." That matters for names and international text, and you'll see a Unicode-aware alternative in the last module.
A caret right after the opening bracket negates the class: [^0-9] matches any one character that is not a digit, including letters, spaces, punctuation and even newlines. [^aeiou] matches any character that isn't a lowercase vowel.
Negated classes are more useful than they first appear. To match the contents of double quotes, "[^"]*" says "a quote, then any number of characters that are not quotes, then a quote." That turns out to be more reliable and faster than alternatives you'll meet in the module on greedy matching.
A caret anywhere else in the class is a literal: [a^] matches "a" or "^."
Step 5 of 14Compare
Same symbol, different meaning inside [ ]
| Aspect | Inside a class | Outside a class |
|---|---|---|
| ^ | First in [^...]: NOT these characters | Start of the string |
| - | Between two characters: a range | Just a dash |
| . | A literal dot | Any character |
| + * ? | Literal characters | Quantifiers |
Step 6 of 14Practice · Sort into groups
Does each character match the class [^a-f0-3]?
Tap an item, then tap its group (or drag it there).
Step 7 of 14Learn
Shorthand classes
A few classes are so common that they have shortcuts:
| Shorthand | Meaning | Roughly equivalent to |
|---|---|---|
\d | digit | [0-9] |
\w | word character | [A-Za-z0-9_] |
\s | whitespace | space, tab, newline and similar |
\D | not a digit | [^0-9] |
\W | not a word character | [^A-Za-z0-9_] |
\S | not whitespace | anything except whitespace |
The uppercase versions are the negations of the lowercase ones. Shorthands can go inside brackets too: [\d.] matches a digit or a literal dot, handy for numbers with decimals.
Two surprises are worth remembering. First, \w includes the underscore and digits, so "user_42" is entirely word characters, but a hyphen is not. Second, in some languages and modes, \d and \w also match non-English digits and letters. Python 3, for example, matches Arabic-Indic digits with \d by default on text strings. If you need strictly ASCII digits, [0-9] is the explicit choice.
The pattern from the first lesson, \d{4}-\d{2}-\d{2}, uses \d for each digit position; the braces, coming up next, say how many times.
Step 8 of 14Practice · Diagram
How \d, \w and \s overlap
Tap the region where the character 7 belongs.
Tap the shape that answers the question, then press Check.
- \w word characters
- \d digits
- \s whitespace
- \w word characters only
Letters and the underscore are word characters but not digits.
- a–z
- A–Z
- _
- \d digits + \w word characters (overlap)
Every digit is also a word character.
- 0–9
- \s whitespace only
No overlap with \w or \d.
- space
- tab
- newline
Step 9 of 14Practice · Match the pairs
Match each shorthand class to a character it matches.
Tap an answer, then tap the place where it belongs (or drag it there).
\d\s\w[^\w\s]
Step 10 of 14Note
Step 11 of 14Practice · Code challenge
Write countDigits(text) that returns how many digit characters (0–9) the text contains. Use a regex with the global flag and String.prototype.match. Remember that match returns null when there are no matches.
countDigits("Room 101, floor 3")countDigits("no numbers here")countDigits("2026-03-14")countDigits("")
Step 12 of 14Practice · Spot the mistake
A teammate claims each of these patterns matches the text "A-7". Tap the two patterns that do NOT match it.
Step 13 of 14Note
Step 14 of 14Practice · Choose one
Which pattern matches a single hexadecimal digit (0–9 or a–f, in either case)?
Lesson complete!