JavaScript Regular Expressions / lesson 4 of 11

Character Classes

Character classes let you match sets of characters using square brackets.

Play
Transcript

[00:00] Character classes in regular expressions allow us to identify sets of characters that we’re willing to accept as part of our match. So I’ve got this string here with cat, mat, bat, hat, question mark at, and zero at, and I’m accepting all of the ats and we can see those are working. And if I want to accept any character before the a, t, that works as well.

[00:27] Now to use a character class, we instantiate that with a set of square brackets. And I’m going to say, I want to accept b and c before at, and I get cat and bat. And the order doesn’t matter here. We just plug in the letters and now I’ve got the exact same thing. We can also negate these.

[00:48] So I could say, I don’t want to get c or b by adding a caret at the beginning of that character class. When I save that, I’m going to get all of the other ones that don’t start with c or b. We could also do a range. So if I want to get all of the ats that are preceded by a lowercase a through lowercase z. So the entire alphabet. Save that and we get cat, mat, and bat.

[01:12] And of course we can also negate those and we get the other three. Now, if I want to include capitals, capitals, so this will be a second range. I’m including a through lowercase z. We don’t do any special syntax here. We just do capital A through capital Z. And now we get cat, mat, bat, and the capital H on hat.

[01:34] Now these don’t have to be full ranges. So let’s just say on the lowercase, I want to get a through D and save that. And now we no longer get mat because it falls outside of the ranges that I’ve specified. We’ll go ahead and put that back to Z. And now if I want to get digits, I just add those zero to nine. I want to get all the digits. So now I’ve got all the alphanumeric values that precede a T, both lowercase and uppercase.

[02:00] The only one I don’t have is that question mark. And we can just union that in as a single character. So I just say question mark right there in the character class. And our character class is everything between lowercase a and lowercase z. Everything between uppercase a and uppercase z. Everything between zero and nine and a question mark. When I save that, we have all the character classes defined as we see fit. And we’ve been able to capture all the items in our string.

Character classes let you match sets of characters using square brackets. This lesson covers the fundamentals: listing specific characters, negating with a caret (^), defining ranges like a-z or 0-9, and combining multiple ranges. You’ll learn how to build flexible patterns that match any character from a defined set — whether it’s letters, digits, or a mix.

app.js

app.ts
import output from "./output.js";
const str = `cat mat bat Hat ?at 0at`;
// Character classes in regular expressions allow
// us to identify specific sets of characters
// that we're willing to accept as part of
// our matches
let regex = /at/g;
regex = /.at/g;
regex = /[bc]at/g;
regex = /[cb]at/g;
regex = /[^cb]at/g;
// character class ranges
regex = /[a-z]at/g;
// union character class ranges
regex = /[a-zA-Z]at/g;
// union character class partial ranges
regex = /[a-dA-Z]at/g;
// negate character classes
regex = /[^a-zA-Z]at/g;
// digits
regex = /[a-zA-Z0-9]at/g;
// union the ? char
regex = /[a-zA-Z0-9?]at/g;
output(str, regex);

Share this post on:

Previous
Quantifiers
Next
Shorthand Unicode Properties