Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSLINT: Bad escapement

JSLint is complaining about the following:

JSLINT: Bad escapement. var r = new RegExp("^\s*" + s + "\s*$", "i");

Can anybody please explain what's wrong the escapement?

like image 979
YoussefTaghlabi Avatar asked Nov 20 '25 10:11

YoussefTaghlabi


1 Answers

You need to double the backslashes.

The string constants in that expression (the expression whose value is passed to the RegExp constructor) are interpreted as such before the regular expression parser sees them. Backslashes are meta-characters in the string constant syntax. Thus, if you don't double them (that is, if you don't express them as backslash-quoted parts of the string), the regular expression parser won't see them at all.

Thus if "s" is "hello world", your code would be equivalent to:

var r = /^s*hello worlds*$/i;

That is, a regular expression that matches zero or more instances of the letter "s", followed by the search string, followed by zero or more letter "s" characters to the end of the string.

like image 124
Pointy Avatar answered Nov 21 '25 22:11

Pointy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!