Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad escapement JSHint

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

var regexS = '[\?&]' + name + '=([^&#]*)';
like image 973
user1790061 Avatar asked Dec 26 '22 15:12

user1790061


2 Answers

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.

like image 190
Ian Avatar answered Dec 29 '22 07:12

Ian


\? 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.

like image 37
Good Person Avatar answered Dec 29 '22 08:12

Good Person