user1790061
user1790061

Reputation: 49

Bad escapement JSHint

Does anybody know why I get a Bad escapement error on JSHint using the follow code?

var regexS = '[\?&]' + name + '=([^&#]*)';

Upvotes: 4

Views: 3012

Answers (2)

Good Person
Good Person

Reputation: 1433

\? isn't a valid escape character. Try replacing it with \\.

So it looks like:

var regexS = '[\\?&]' + name + '=([^&#]*)';

Keep in mind that "\" escapes the character that comes after it. This is why \\ comes out as a single slash (if you look at the source of this question you will find I needed to quadruple the \).

Other common escape sequences are \n for newline and \t for tab.

Upvotes: 4

Ian
Ian

Reputation: 50905

Just double escape the \

var regexS = '[\\?&]' + name + '=([^&#]*)';

Even though I'm guessing you'll be using this string for a Regex object, characters in a string must be escaped correctly. By default, a \ attempts to escape the next character. If you add an extra one to be like \\, it escapes the original \ and evaluates to a single \ in the final string.

Upvotes: 6

Related Questions