Reputation: 456
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?
Upvotes: 3
Views: 861
Reputation: 413702
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.
Upvotes: 4