Character classes
A character class, written in square brackets like [abc], matches exactly one character from a set. This page covers sets, ranges, negation, which characters are special inside the brackets, and the cases that fail to compile. For the rest of the syntax, see Pattern syntax.
All examples use the default options (dot: true, caseInsensitive: false) unless a table says otherwise.
Basic forms
| Form | Matches one character that is… |
|---|---|
[abc] | a, b, or c |
[a-z] | in the range a to z |
[abc0-9] | a, b, c, or a digit (sets and ranges mix) |
[!abc] | not a, b, or c |
[^abc] | not a, b, or c (same as [!abc]) |
| Pattern | Path | Match? |
|---|---|---|
*.[ch] | main.c | yes |
*.[ch] | util.h | yes |
*.[ch] | main.cpp | no |
*.[jt]s | app.ts | yes |
[Rr]EADME.md | README.md | yes |
[Rr]EADME.md | ReadMe.md | no |
file[0-9].txt | file1.txt | yes |
file[0-9].txt | file10.txt | no |
[a] | a | yes |
[a] | [a] | no |
A class always matches exactly one character, never zero and never two. file[0-9].txt does not match file10.txt.
A class with one member is still a class: [a] matches a. Braces work differently: a brace with no comma, like {a}, is plain text (see Brace expansion).
Spec: GLOB_SPEC §8.1
Ranges
[x-y] matches any character from x to y, both included. Ranges compare byte values, so they are case-sensitive by default: [a-z] does not match X. With caseInsensitive: true, letters match in either case, so [A-Z] also matches a. See How a path is matched.
A range must go up. If the end comes before the start, the pattern fails to compile with InvalidRange. This check looks at the bytes you wrote, so [a-A] is an error even with caseInsensitive: true.
| Pattern | Result |
|---|---|
[0-9] | any digit |
[a-zA-Z0-9_] | a letter, digit, or _ |
[a-a] | just a |
[z-a] | InvalidRange error |
[9-0] | InvalidRange error |
[a-A] | InvalidRange error |
Spec: GLOB_SPEC §8.1, §13
Negated classes
A ! or ^ right after the [ flips the class: it matches one character that is not in the set. The two forms mean exactly the same thing.
| Pattern | Path | Match? |
|---|---|---|
[!abc] | d | yes |
[!abc] | a | no |
[^abc] | d | yes |
[^abc] | a | no |
[!_]*.ts | index.ts | yes |
[!_]*.ts | _internal.ts | no |
Negated classes and dot
With dot: false, a negated class at the start of a segment does not match a leading .. This keeps dot files hidden, the same way * and ? do. A class that lists . as a member still matches it, and in the middle of a name the rule does not apply.
| Pattern | Path | dot: true | dot: false |
|---|---|---|---|
[^x]* | .env | yes | no |
src/[^_]*.ts | src/.hidden.ts | yes | no |
src/[^_]*.ts | src/main.ts | yes | yes |
[.]* | .env | yes | yes |
x[^_] | x. | yes | yes |
Spec: GLOB_SPEC §8.4
Classes never match /
A class lives inside one path segment. It never matches a separator, not even a negated class: [!x] matches almost anything, but not /.
| Pattern | Path | Match? |
|---|---|---|
a[!x]b | ayb | yes |
a[!x]b | a/b | no |
Because of this, a / inside the brackets is a compile error, UnterminatedClass. A / ends the segment, so the class never sees its ]. This holds for [/], [a/b], [^/], and the escaped form [\/]. An error here is better than a pattern that compiles but can never match.
A literal backslash is allowed as a member: [\\] is the class that holds \. On Linux and macOS it matches a \ in a file name. On Windows, \ is a path separator, so this class never matches there.
Spec: GLOB_SPEC §8.2
Special characters inside a class
Inside the brackets, most meta-characters lose their meaning. Only these are special:
| Character | Inside a class |
|---|---|
] | closes the class, unless it comes first (see below) |
- | makes a range between two characters; plain - at the start or the end |
!, ^ | negation, only right after [ |
\ | escapes the next character |
/ | not allowed (compile error) |
* ? { } ( ) @ + | plain characters |
| Pattern | Path | Match? |
|---|---|---|
[?*!(] | ? | yes |
[?*!(] | * | yes |
[?*!(] | a | no |
[-abc] | - | yes |
[abc-] | - | yes |
[a-] | - | yes |
[!abc] | ! | yes |
Escapes inside a class
A \ makes the next character a plain member, so \- is a dash and not a range, and \! is a member and not negation.
| Pattern | Path | Match? |
|---|---|---|
[a\-z] | - | yes |
[a\-z] | b | no |
[\!abc] | ! | yes |
[\!abc] | d | no |
[\?] | ? | yes |
[\]] | ] | yes |
[\] is an error: the \ escapes the ], so the class never closes (UnterminatedClass).
Spec: GLOB_SPEC §8.3
A ] right after [, and empty classes
A ] that comes first in a class (right after [, [!, or [^) is a plain member, not the closer. This is the POSIX rule. It lets you match ] without an escape, and it means there is no such thing as an empty class.
| Pattern | Result |
|---|---|
[]] | matches ] |
[!]] | matches any one character except ] |
[]-] | matches ] or - |
[]-a] | a range from ] to a: matches ], ^, _, ` or a |
[] | UnterminatedClass error: the ] is a member, and nothing closes the class |
[!], [^] | UnterminatedClass error, for the same reason |
[ | UnterminatedClass error |
The first ] is a member like any other, so it can start a range, as POSIX reads it. The same rule is why [!]foo fails to compile. Write [\!]foo or \!foo instead.
A class takes one byte. A non-ASCII character in a class stands for its UTF-8 bytes, each a member of its own, so [é] never matches é (two bytes). To match one of several characters, use a brace: {é,è}.
Spec: GLOB_SPEC §8.5
Classes and braces
You can put a class inside a brace branch, and both work as usual.
| Pattern | Path | Match? |
|---|---|---|
{[0-9],x}.log | 1.log | yes |
{[0-9],x}.log | x.log | yes |
{[0-9],x}.log | 10.log | no |
*.[jt]s{,x} | app.tsx | yes |
Inside a class, {, }, and , are plain characters, so [{]} matches the path {}. Whichever bracket opens first owns the text until its own closer. In {[}], the class swallows the }, the brace is never closed, and the pattern fails with UnterminatedBrace. See Compile-time errors.
Spec: GLOB_SPEC §10.7, §4.4
Classes match one byte
Non-ASCII characters in a class
A class matches one byte, not one visible character. A character like é takes two bytes in UTF-8, so [é] does not match the path é. Use a brace instead: {é,e} matches either one.
POSIX named classes
Named classes such as [[:alpha:]] and [[:digit:]] are not supported and not recognized. [[:alpha:]] is the class [[:alpha:] followed by a literal ], so it matches a] and never a alone. Write the ranges yourself:
| Instead of | Write |
|---|---|
[[:digit:]] | [0-9] |
[[:alpha:]] | [a-zA-Z] |
[[:alnum:]] | [a-zA-Z0-9] |
Spec: GLOB_SPEC §8.7