admin管理员组

文章数量:1356945

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

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

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

var regexS = '[\?&]' + name + '=([^&#]*)';
Share Improve this question edited Aug 9, 2014 at 17:41 fernandopasik 10.5k7 gold badges51 silver badges56 bronze badges asked Nov 1, 2012 at 0:25 user1790061user1790061 491 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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.

\? 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 es after it. This is why \\ es out as a single slash (if you look at the source of this question you will find I needed to quadruple the \).

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

本文标签: javascriptBad escapement JSHintStack Overflow