admin管理员组文章数量:1345452
I have a Javascript error that have been reported to me and unfortunately I've no idea on how to reproduce it. IE8 Developer Tools reports the following error: Unexpected quantifier. The following row produces the error:
var g=RegExp(d+"=([^|]*)").exec(j); //output from closure-piler
I guess I have to escape properly the pipe like that (\\|
) to fix the problem, but I don't know if I'm right because I don't know how to reproduce the error.
Any suggestions or solutions are wele.
Thanks.
[UPDATE]
The values of d
are the keys of __utmz
cookie values which I'm trying to retrieve and they are listed in an array like this ["utmccn", "utmcmd", /* ... */]
. Well now there's not so much I can do, I'm at home with my friend the flu.
I have a Javascript error that have been reported to me and unfortunately I've no idea on how to reproduce it. IE8 Developer Tools reports the following error: Unexpected quantifier. The following row produces the error:
var g=RegExp(d+"=([^|]*)").exec(j); //output from closure-piler
I guess I have to escape properly the pipe like that (\\|
) to fix the problem, but I don't know if I'm right because I don't know how to reproduce the error.
Any suggestions or solutions are wele.
Thanks.
[UPDATE]
The values of d
are the keys of __utmz
cookie values which I'm trying to retrieve and they are listed in an array like this ["utmccn", "utmcmd", /* ... */]
. Well now there's not so much I can do, I'm at home with my friend the flu.
- 3 the unexpected quantifier is probably ing from the value of variable d, as there is nothing wrong with the rest of that regex – CrayonViolent Commented Feb 7, 2011 at 16:26
- I assume if you can't reproduce the error, the logic that generates d should be looked at more closely. Why don't you post that logic? – user557597 Commented Feb 7, 2011 at 20:24
- similar problem and relative solution, hope can help stackoverflow./questions/8137741/… – GibboK Commented Nov 15, 2011 at 14:35
2 Answers
Reset to default 6A "quantifier" is a regular expression operator like *
or +
. The variable "d" in the above code probably contains something that's being interpreted as "code" to the regular expression parser, and it's invalid. You're right that it needs to be appropriately escaped. It's a problem very similar to the problem of escaping text that's being included in HTML markup, but distinct because the syntax in question is pletely different (that is, regular expression syntax vs. HTML syntax).
There are a lot of special characters in regular expression syntax, obviously, so it'll take a pretty gnarly regex to do that. Here's a possible way to do such a thing:
var quoted = unquoted.replace(/([*+.?|\\\[\]{}()])/g, '\\$1');
That might not get all of them; I'd look around for code that's more definitely tested than something a dope like me just made up :-)
Since that code you posted looks like it may e from some library, you may want to check the stack trace from the IE debugger to find out where the problem starts.
As a general rule you should always escape special characters inside character classes, you never know when you run into a regexp engine that will fail.
本文标签: javascriptWhy RegExp generates an error of quotunexpected quantifierquot on IE8Stack Overflow
版权声明:本文标题:javascript - Why RegExp generates an error of "unexpected quantifier" on IE8? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743787817a2539011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论