[abc]: character sets and what loses its meaning inside
[abc] matches exactly one character from the listed set. Inside the brackets almost every metacharacter becomes literal, so only the closing bracket, caret, hyphen and backslash still need care.
At a glance
- Syntax
- [abc]
- Matches
- exactly one character from the set
- Literal inside
- . * + ? ( ) { } | and the dollar sign
- Still special inside
- ] ^ - and the backslash
- Empty class
- [] matches nothing at all, it is not a wildcard
- Negated form
- [^abc]
In detail
A set is an alternation of single characters with none of the cost. Writing [aou] instead of (?:a|o|u) is shorter, faster and cannot accidentally capture anything, so any alternation whose branches are all one character long should be a set instead. Order inside the brackets is irrelevant and duplicates are harmless.
The relief of a character class is that the metacharacters stop biting. A dot is a full stop, a plus is a plus, and parentheses and braces are just themselves. That is why [.+*] matches a literal dot, plus or asterisk with no escaping at all, and why building a class is usually easier than escaping a run of punctuation.
Four characters keep meaning. The closing bracket ends the class, so a literal one must be escaped as [\]] rather than relying on position. The caret negates in first position only. The hyphen makes a range unless it is first, last or escaped. The backslash still introduces escapes, which is how \d and \n keep working inside a class.
The empty class is a genuine trap for anyone generating patterns. [] is legal and matches nothing whatsoever, so a pattern built from an empty list of options never matches rather than matching everything. Generating code should check the list before it writes the brackets.
Examples
"cat cot cut".match(/c[aou]t/g); // ["cat", "cot", "cut"]
const m = /[.+*]/.exec("a+b");
m[0]; // "+"
m.index; // 1/[\]]/.test("]"); // true, a literal closing bracket must be escaped
/[a^]/.test("^"); // true, the caret is literal when it is not first
/[]/.test("a"); // false, an empty class matches nothingThings that catch people out
- A closing bracket inside a class must be escaped as \]. Relying on position does not work in JavaScript the way it does in some other engines.
- An empty class produced by generated code matches nothing rather than everything, so the resulting pattern fails on every input with no error to explain it.
- Escaping metacharacters inside a class is harmless but obscures which of the four actually needed it, which makes the next edit riskier than it should be.
Try it: Regex Tester
Type a pattern and a test string, watch every match highlight as you go, read a plain English breakdown of each token, and check the numbered and named groups it captured.
Regex Tester
LOCAL · Browser-OnlyTest and debug regular expressions with live matching and highlights
Related Regex syntax
All regex syntax →[^abc]: negated character sets that quietly cross newlines
A negated class matches one character that is not in the set, including line terminators. That is why a quoted string pattern runs across lines.
[a-z] ranges: code point order and the A-z bug
Ranges inside a character class run by code point, which is why [A-z] quietly includes six punctuation characters and [a-Z] is a syntax error.
Escaping metacharacters: which ones, and where
Twelve characters need escaping outside a character class and only four inside one. User input needs an escape function before it goes near new RegExp.
\p{...}: Unicode property escapes, and the flag they need
Property escapes match by Unicode category or script instead of by code point range. Without the u flag they compile into something else entirely.
\b: word boundaries, underscores and accented letters
\b is a zero width assertion defined entirely in terms of \w, which is why user_name has no internal boundary and café has none at its end.
\B: the non word boundary and what it is good for
\B asserts that a position is not a word boundary, which turns it into an inside a word search. It also never matches at the edges of a word.
Tools for this
- Regex Tester Free online regex tester - test and debug regular expressions with live matching and highlights
- Regex Playground Regex playground with live match highlighting, named groups, find and replace, and shareable links