Regular Expressions: Escaping Characters
How do you escape a special character in a regular expression?
Interview Response: To match a character having a special meaning in regex, you need to use an escape sequence prefix with a backslash (\). E.g., \. matches "."; regex \+ matches "+"; and regex \( matches "(". You also need to use regex \\ to match "\" (back-slash). Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.
Is a forward slash “/” a special character in regular expressions?
alert('/'.match(/\//)); // '/'
// Using the RegExp Object
alert('/'.match(new RegExp('/'))); // finds /
When we are calling the new RegExp object, do we have to use the escape character in our regular expressions?
Interview Response: If we create a regular expression with the new RegExp, we do not have to escape /, but need to do other escaping like the `\d` flag. The reason that this happens is that strings consume backslashes when we use the RegExp object constructor. The approach we should use is an additional backslash to escape special flags like \d. Instead of \d, we need to use `\\d`.
// Wrong Approach
let regexp = new RegExp('d.d');
alert('Chapter 5.1'.match(regexp)); // null, when we ant 5.1
// Correct Approach
let regexp = new RegExp('\\d.\\d');
alert('Chapter 5.1'.match(regexp)); // returns 5.1