RegEx Series: Special Characters (Part 1)
I have introduced ranges in this post which is a great and efficient way to go about writing out for example matches that are within a span of consecutive characters rather than having to manually write each one of them out. In this post, I will be mentioning another great feature of regex that are called metacharacters.
Metacharacters in regular expressions help match characters based on what catergories they may fit into. For example,
\d will match any digit character that is (0–9). You can use metacharacters like
\d in your regex like:
make sure to have the backslash when using the metacharacters.
/ [\metacharacterHere] / (optional flags here)
Some Common Metacharacters
Let’s look at some examples of these in action!
Similar to the example I mentioned above with the
\d example, It matches all of the numbers but it does not match the ‘w’ character.
In this example, the first character in the regex is looking for any word character and then the second character is looking for whitespace which is why it matches the ‘I ’ and the space before the ‘am.’
These are some ways you can go about using metacharacters in your regular expressions to make your patterns easier to match.
In the part two, I will show how to combine quantifiers with these metacharacters as well as other special symbols that will make our matches better! See you in Part Two!