Most regex confusion starts in the same place: the square brackets. A pattern behaves unexpectedly, and the cause is usually a character class doing exactly what it was told, which was not quite what its author meant. Classes are how a pattern stops naming one exact character and starts allowing a set: any vowel, any digit, anything except a digit. Get classes right and the rest of regex gets easier to predict.
This guide explains classes piece by piece, with a small tested example for each idea, using the free Regex Tester on Ovatly, which runs a JavaScript regular expression against your text and lists every match with its position and capture groups.
Contents
- Quick answer
- A class is a set, and it matches one character
- Ranges: spans of characters
- Negation: everything except the set
- The predefined classes and their capitals
- Escaping rules inside a class
- How to test a class, step by step
- Worked example
- Limitations
- Common mistakes
- FAQs
- Conclusion
Quick answer
A character class is a set written in square brackets, and it matches exactly one character from that set: [aeiou] matches one vowel. A hyphen between two characters makes a range, so [a-z] matches one small letter and [0-9] one digit. A caret as the first character negates the class: [^0-9] matches one character that is not a digit. Three predefined classes cover the common sets: \d for a digit, \w for a word character (letters, digits and underscore), \s for whitespace, and the capitals \D, \W and \S match the opposite of each. Inside a class, a dot is a literal dot and a hyphen at either end is a literal hyphen.
A class is a set, and it matches one character
Write the characters you will accept between square brackets: [aeiou]. The class is a set, and the order inside it does not matter. Wherever the text holds one of those characters, the class matches it.
Run [aeiou] with the g flag against regular expression and the tester lists seven matches: e at index 1, u at 3, a at 5, e at 8, e at 12, i at 15 and o at 16. Every match is one character long. The class never matches the sequence io as a unit; it finds the i, then separately the o. For several characters in a row, a quantifier does that job: [aeiou]+ matches the whole io run as one match.
Ranges: spans of characters
Listing every letter of the alphabet in a class would be tedious, so a hyphen between two characters means the span between them. [a-z] is every small letter, [A-Z] every capital, [0-9] every digit. Ranges combine freely in one class: [A-Za-z] accepts any letter of either case, and [A-Za-z0-9] accepts letters and digits together.
Two rules govern ranges. The span follows character order, so the start must come before the end: [a-z] works, while a backwards span is an invalid pattern and the tester will say so in plain language. And a range covers only what it names: [a-z] knows nothing about accented letters or capitals. For capitals too, add the second range or use the i flag.
Negation: everything except the set
A caret as the first character inside the brackets negates the class: [^0-9] matches any one character that is not a digit. Run it with g against abc123 and it matches a at index 0, b at 1 and c at 2, then stops: the digits are what it refuses.
Position is everything: a caret anywhere else in the class is just a caret character. And negation is broad: [^0-9] also matches spaces and punctuation, because everything except digits is a large set. When you mean letters only, write the letter range instead.
The predefined classes and their capitals
Three sets come up so often that regex gives them one-letter shortcuts, written with a backslash:
| Shortcut | Means | Same as |
|---|---|---|
\d | One digit | [0-9] |
\w | One word character | [A-Za-z0-9_] |
\s | One whitespace character | spaces, tabs, line breaks |
Capitalising the letter negates the shortcut: \D is any non-digit, \W any non-word character, \S any non-whitespace character. The one to internalise is \w: its set includes digits and the underscore alongside letters, so it accepts far more than "word" suggests.
Escaping rules inside a class
Inside brackets, most punctuation loses its special meaning, so it needs no escape. A dot is the clearest case: in a pattern at large, . matches almost any character, but in [.] it matches only a full stop. Run [.] against v1.2 and it matches the . at index 2, nothing else.
The hyphen has a split personality. Between two characters it builds a range; at the very start or end of a class it is a literal hyphen. That is why the separator class [ -] works: the dash sits at the end, so it means itself. A mid-class hyphen meant literally can also be escaped as \-. Two characters still need care: the closing bracket, which ends the class unless escaped as \], and the backslash, which keeps its escaping job, so [\d] means the digit set.
How to test a class, step by step
- Open the Regex Tester.
- Type your class in the Pattern field, without slashes, for example
[aeiou]. - Put
gin the Flags field. Without it, only the first match is reported, and a class lesson needs the full list. - Paste a short test text whose result you can predict, such as
regular expression. - Read the match list: each entry shows the matched character and its index. Compare it with your prediction.
Worked example
The tester's own example shows a predefined class at work. Pattern \d+ with flag g, against Order 123 ships in 45 days, reports two matches: 123 at index 6 and 45 at index 19. Read it as two ideas combined: \d is the digit class, and + repeats it, so each match is a whole run of digits. Without the plus, the same text would produce five one-digit matches.
Set that beside the vowel run from earlier: [aeiou] gave seven separate one-character matches, while [aeiou]+ would merge the neighbouring i and o into one. The class decides which characters qualify; the quantifier decides how many in a row form one match. Nearly every class bug is one of those two decisions made wrong.
Limitations
A class matches one character position, with no memory: it cannot express ideas like "a digit followed later by the same digit", which need groups and backreferences. Ranges follow fixed character order, so they cover plain letters and digits well and everything else poorly; accented characters must be listed individually. The tester runs JavaScript's RegExp engine, whose class behaviour differs slightly from PCRE, Python or grep flavours, so retest in your own stack. And it stops listing at 500 matches, which matters when a broad class meets a long document.
Common mistakes
- Expecting
[abc]to match the wordabc. It matches one character: ana, abor ac. - Putting the caret in the middle. Only a first-position caret negates; elsewhere it is a literal caret.
- Creating a range by accident. A hyphen between two characters means a span. For a literal hyphen mid-class, move it to an end or escape it.
- Forgetting what
\wcontains. Digits and underscores are word characters, so\w+matchesabc_123whole. - Negating when you mean selecting.
[^0-9]accepts spaces and punctuation too. Name the set you want instead.
FAQs
Does a class like [abc] match the word abc?
No. A class matches exactly one character, so that class matches a single a, b or c wherever one appears. To match several characters in a row, add a quantifier: the plus sign after the class repeats it one or more times.
How do I match any digit? Either write the range zero to nine inside brackets, or use the predefined digit class, backslash d. The two are identical in JavaScript regex. The capital version matches any character that is not a digit.
Why does a negated class match letters and spaces too? Because negation means everything except the set, with no other limits. A class that excludes digits accepts letters, spaces and punctuation alike. If you want letters only, say so with a letter range instead of negating the digits.
How do I match a literal dot inside a class? Just put the dot in the class. Inside brackets a dot loses its special meaning and matches only a full stop, so a class holding only a dot finds the dot in a version string. Outside a class you would need to escape it with a backslash.
Does the word class include digits? Yes. The predefined word class covers capital and small letters, the digits zero to nine, and the underscore. That breadth surprises people: a pattern built from it happily matches numbers and underscores, not just letters.
Conclusion
Character classes are where a regex describes a set instead of a literal string: brackets to list the set, a hyphen for a range, a caret first to negate it, and \d, \w and \s as shortcuts for the sets used most. Practise in the Regex Tester with the g flag on, predicting each match before you run it, and class behaviour quickly becomes second nature. Classes are also the load-bearing piece of the card number patterns in How to Validate a Credit Card Number Format with Regex, where the separator between digit groups is a small class of its own. More guides live in the Developer Tools archive.