Character Classes
It’s possible to match a character from within a set of characters.
/[aeiou]/g
matches all vowels in our input strings.
Here’s another example of these in action:
We match a p
, followed by one of the vowels, followed by a t
.
There’s an intuitive shortcut for matching a character from within a continuous range.
The regex /[a-z]/g
matches only one character. In the example above, the strings have several matches each, each one character long. Not one long match.
We can combine ranges and individual characters in our regexes.
Our regex /[A-Za-z0-9_-]/g
matches a single character, which must be (at least) one of the following:
- from
A-Z
- from
a-z
- from
0-9
- one of
_
and-
.
We can also “negate” these rules:
The only difference between the first regex of this chapter and /[^aeiou]/g
is the ^
immediately after the opening bracket. Its purpose is to negate the rules defined within the brackets. We are now saying:
“match any character that is not any of
a
,e
,i
,o
, andu
”