RegEx Series: RegEx Using Sets
This post is going to continue off of what I discussed in this blog post. We are going to be looking at using character sets which is extremely useful for matching patterns you may want to find that are not literal matches.
How to use character sets is by placing the pattern you want in square brackets:
/[<insertPatternHere>]/(optional flags here)
Rather than searching for literal strings, we can place certain characters into the brackets we may want to look for, like for example if I want to match any word that starts with the letter ‘d’ or ‘s’ and has the ending ‘og’.
Using Character Sets to Match Characters
As you can see above, (I have the global flag on so that I can show you both cases) the pattern within the character set is saying, ‘look for anything that starts with a “d” or “s” and is followed by “og”. The “og” is outside of the character set so that means it's literal and the character set will determine if that pattern will be found depending on the beginning letter. If my test string instead had ‘tog’, then there would be no match at all.
Now you might be asking, “ok great there are all these options for matching characters, but what if I want to create an exclusion match?” In other words, is it possible to match everything except ‘a’?
The answer is YES.
Using Character Sets to Exclude Characters
The way you would do this is by adding a carat symbol inside of the square brackets:
/[^<insertPatternHere>]/(optional flags here)
It matches the ‘dog’ and the ‘sog’ part of the test string but not the ‘aog’ because we have told it to exclude any ‘og’ string that starts with the letter ‘a’.
Using character sets is very useful and can be an easier way to match specific patterns that may not be literal. For example, if you come across an algorithm problem on code wars or leet code asking you to find the number of vowels in a string, this is one clever way you could go about approaching that problem. Hope that was helpful.