Escaping Meta-Characters
Learn about escaping the special characters.
Overview of escaping meta-characters
Consider the RegEx, /.*?@xyz\.com/
, it is used to match xyz.com
. Note that the domain backslash (\
) is used after “xyz” and before “.com”. To match the dot as a literal character, you need to escape it. If not escaped, it will match any one character except the new line. For example, /email.com/
will match "email.com"
, “email_com”, and “email@com”. If you want to match email.com
, the RegEx will be /email\.com/
. The character just after the backslash has its real meaning).
The backslash tells the RegEx engine that the next character has escaped. Don’t consider its special meaning. Try not to escape a literal character because escaping it could change the meaning. If you are familiar with the \t
, it is used as a tab in many programming languages. So if you use a backslash before the character “t”, it will act as a tab character. Similarly, if you use a backslash before the character “n”, it will be considered as a line break by the RegEx engine. And in some cases, a backslash is ignored.For instance, “\z” will be considered as literal “z”. Not only that, but it can extend to multiple literal characters as well. Be very cautious when using escape with literal characters if at all.
Get hands-on with 1400+ tech skills courses.