-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Unnecessary Escape warning
Tyler Breisacher edited this page Mar 10, 2017
·
2 revisions
If you're writing a string intended to be used as a regular expression you likely have special RegExp escapes in it, such as \s or \w. You may see a warning saying something like "Unnecessary escape: '\w' is equivalent to just 'w'". This is because, while \w is a valid escape sequence in regular expressions, it isn't in string literals. In a string literal, \w just means w. To get the RegExp escape \w you need \\w. See this jsfiddle link for a demo.
In some cases, it might be much simpler to write your code using a /RegExp literal/ instead of a 'string literal' to remove the need for a lot of backslashes.