JSLINT: bad descent

JSLint complains about the following:

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

Can someone explain what happened to the escape?

+3


source to share


1 answer


You need to double the backslash.

The string constants in this expression (the expression whose value is passed to the RegExp constructor) are interpreted as such before the regular expression parser sees them. Backslashes are metacharacters in string constant syntax. That way, if you don't double them (that is, if you don't express them as slash parts in a string), the regex parser won't see them at all.

Thus, if "s" is "hello world", your code is 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 a search string, followed by zero or more "s" characters at the end of the string.

+4


source







All Articles