You will be able to use JavaScript regular expression character classes to match one character from a set, a range, or a shorthand group such as digits or word characters.
What it is
A character class is written inside square brackets, such as [aeiou]. It matches exactly one character from the allowed set. A range like [a-z] means every lowercase letter from a to z. A leading caret, [^aeiou], negates the class and matches one character that is not in the set. JavaScript also provides shorthand classes: \d for digits, \w for word characters, and \s for whitespace. Related terms include quantifiers, flags, and alternation.
Why it matters
- Validate user input, such as checking whether a field contains only letters.
- Extract specific characters, such as vowels, digits, or punctuation.
- Replace unwanted characters, such as removing non-alphanumeric text.
- Build compact patterns instead of listing many alternatives.
- Support case-insensitive matching when combined with the
iflag.
Syntax or steps
The smallest useful pattern is:
/[aeiou]/g
Read it as: match one character from the set a, e, i, o, or u, and do it globally.
[abc]matches one ofa,b, orc.[a-z]matches one lowercase letter.[A-Za-z0-9]matches one alphanumeric character.[^0-9]matches one non-digit character.\d,\w, and\sare shorthand classes.
Inside a character class, most symbols are literal, but ^, -, ], and \ have special meaning. Put - first or last, or escape it, if you want a literal hyphen.
Example
const text = "Hello, World! 2024";
const vowels = /[aeiou]/g;
const digits = /\d/g;
const nonAlphanumeric = /[^A-Za-z0-9]/g;
console.log(text.match(vowels));
console.log(text.match(digits));
console.log(text.match(nonAlphanumeric));
Expected output:
[ 'e', 'o', 'o' ]
[ '2', '0', '2', '4' ]
[ ',', ' ', '!', ' ' ]
Part by part:
/[aeiou]/gfinds each lowercase vowel. Thegflag returns all matches, not just the first./\d/gfinds each digit./[^A-Za-z0-9]/gfinds each character that is not a letter or digit.text.match(...)returns an array of matched characters, ornullif there are no matches.
Common mistakes
- Forgetting the
gflag when you need every match. Withoutg,matchreturns only the first match. - Using
[a-z]when uppercase letters should also match. Fix it by using[A-Za-z]or adding theiflag. - Placing
-in the middle of a class accidentally, such as[a-z-0-9]. If you want a literal hyphen, put it at the end:[a-z0-9-]. - Thinking a class matches multiple characters.
[aeiou]matches one vowel; use a quantifier like[aeiou]+for one or more.
When to use it
Use a character class when you need to match one character from a set or range. Use alternation when you need to match whole words or multi-character options.
| Need | Use | Example |
|---|---|---|
| One character from a set | Character class | /[aeiou]/g |
| One of several words | Alternation | /cat|dog|bird/g |
| Any digit | Shorthand class | /\d/g |
| Simple literal check | String method | text.includes("cat") |
Practice
Guided exercise: write a pattern that matches every lowercase consonant in "JavaScript".
const word = "JavaScript";
const consonants = /[bcdfghjklmnpqrstvwxyz]/g;
console.log(word.match(consonants));
Expected output:
[ 'v', 'c', 'r', 'p', 't' ]
Challenge: extract all word-like tokens from "user_42, email@site.com". Hint: use \w+. Expected output: [ 'user_42', 'email', 'site', 'com' ].
Quick check
What does /[^0-9]/g match?
It matches every character that is not a digit.
Summary
Character classes let you describe a single allowed character compactly using sets, ranges, negation, or shorthand groups. They are useful for validation, extraction, and replacement, especially when combined with flags and quantifiers.