Level 1: Foundations · Lesson 5 of 30
Character sets [ ]
Square brackets define a set of allowed characters for one position. Use them to match one of several letters, a range like a-z, or a mix of both.
A character set, also called a character class, is written with square brackets. It matches exactly one character, and that character must be one of the characters listed inside. The pattern gr[ae]y matches gray and grey, but not groy, because only a or e is allowed in the third position.
Inside brackets you can write ranges with a hyphen. [0-9] matches any single digit, [a-z] any lowercase letter, and [A-Z] any uppercase letter. You can combine several ranges and single characters in one set: [A-Za-z0-9_] matches a letter, a digit or an underscore. Ranges follow the order of characters in the character table, so always write them from low to high.
Remember that a set still matches only one character. [0-9] matches 7, but on 42 it produces two separate matches. To match a whole number, add a quantifier after the set: [0-9]+. The quantifier repeats the whole set, so each repeated character can be any of the allowed ones.
Most metacharacters lose their special meaning inside brackets. In [.+?], the dot, plus and question mark are all literal. The characters that still need care are the closing bracket ], the backslash \, the caret ^ when it comes first, and the hyphen - when it sits between two characters. Put a hyphen first or last, as in [-a-z], to make it literal.
Character sets work the same way in JavaScript, Python, PHP, Java, Go, C#, Rust and Ruby. Some engines add extras, such as POSIX classes like [[:alpha:]] in PHP, Go, Rust and Ruby, but JavaScript and Python do not support those, so the simple bracket syntax is the portable choice.
Sets are a great way to be specific without being rigid. For a grade letter you can write [A-F], for a hexadecimal digit [0-9a-fA-F], and for a room number between one and three room[1-3]. Each of these says exactly what you accept, which makes the pattern easier to read and less likely to match junk.
A caret right after the opening bracket flips the meaning of a set, matching anything except the listed characters. That idea gets its own lesson later, so for now keep the caret out of the first position.
Live tester
2 matches (JavaScript engine)
gray grey groy
Examples
Numbers
/[0-5]+/gdigits 123 789 450
Matches: “123”, “450”
Only digits from 0 to 5 are allowed, so 789 produces no match.
Text
/[aeiou]/gregular expression
Matches: “e”, “u”, “a”, “e”, “e”, “i”, “o”
Each vowel is a separate match because a set matches one character at a time.
Codes
/[A-C][0-9]/gA1 B2 D3 C9
Matches: “A1”, “B2”, “C9”
The first position must be A, B or C, so D3 is skipped.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match both spellings gray and grey, but not groy.
gray grey groy
Task 2: Match two-character hexadecimal tokens made of digits and the letters a to f.
ff 1a zz 9g c3
Task 3: Match room numbers 1, 2 and 3 only.
room1 room2 room3 room8 room9