javascript 为什么 RegExp 在 IE8 上生成“意外量词”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4923652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:22:44  来源:igfitidea点击:

Why RegExp generates an error of "unexpected quantifier" on IE8?

javascriptregex

提问by Minkiele

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:

我有一个已报告给我的 Javascript 错误,不幸的是我不知道如何重现它。IE8 Developer Tools 报告以下错误:Unexpected quantifier。以下行产生错误:

var g=RegExp(d+"=([^|]*)").exec(j); //output from closure-compiler

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 welcome.

欢迎任何建议或解决方案。

Thanks.

谢谢。

[UPDATE]

[更新]

The values of dare the keys of __utmzcookie 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.

的值d__utmz我试图检索的cookie 值的键,它们列在这样的数组中["utmccn", "utmcmd", /* ... */]。好吧,现在我无能为力,我和我的朋友流感在家。

回答by Pointy

A "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 completely different (that is, regular expression syntax vs. HTML syntax).

“量词”是正则表达式运算符,如*or +。上面代码中的变量“d”可能包含一些被正则表达式解析器解释为“代码”的东西,它是无效的。你是对的,它需要被适当地转义。这是一个与 HTML 标记中包含的转义文本问题非常相似的问题,但由于所讨论的语法完全不同(即正则表达式语法与 HTML 语法)而不同。

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, '\');

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 come from some library, you may want to check the stack trace from the IE debugger to find out where the problem starts.

由于您发布的代码看起来可能来自某个库,因此您可能需要检查来自 IE 调试器的堆栈跟踪以找出问题的根源。

回答by joachimm

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.

作为一般规则,您应该始终转义字符类中的特殊字符,您永远不知道何时遇到会失败的正则表达式引擎。