...
/Analyzing Different Types of Regular Expressions
Analyzing Different Types of Regular Expressions
Learn and practice different types of regular expressions.
We'll cover the following...
Character classes
A character class allows us to specify which characters are acceptable for any given position. For example, suppose we had the following pattern:
/abc/
We can interpret it as a literal a followed by a literal b followed by a literal c. Suppose we wanted to match all substrings that contained those three characters in any order. We can achieve this using a character class. We define character classes by specifying what characters are allowed between square brackets:
We can also use negation to specify that we do not want characters to appear at a specific location. We do this by starting our class with the ^ character:
In the code above, our regular expression can be interpreted as:
- The character
a,b, orcfollowed by- Any character that is not
b ore`, followed by- The character
a,b, orc.
- The character
- Any character that is not
If we wanted to include the ^ character itself in our character class, we need to add it to the end of the character class so it is not interpreted as the negation operator:
We can also specify ranges of characters when defining our character classes. This helps to avoid having to write every possible combination of characters. For example, we could rewrite our ...