简体   繁体   English

在JavaScript中尝试使用Gruber的“改进的”URL匹配regexp模式时,如何修复“无效组”错误?

[英]How do I fix “invalid group” error when attempting to use Gruber's “improved” URL matching regexp pattern in JavaScript?

I'm attempting to integrate John Gruber's An Improved Liberal, Accurate Regex Pattern for Matching URLs into one of my Javascripts, but WebKit's inspector (in Google Chrome 5.0.375.125 for Mac) gives an "Invalid group" regular expression syntax error. 我正在尝试整合John Gruber的改进的自由,准确的正则表达式模式,以便将URL匹配到我的Javascripts中,但是WebKit的检查器(在Google Chrome 5.0.375.125 for Mac中)给出了“无效组”正则表达式语法错误。

Gruber's original regexp is as follows: Gruber的原始正则表如下:

(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))

The line from my JavaScript w/the regexp is as follows (w/forward slashes backslash-escaped): 我的JavaScript与regexp的行如下(带正斜杠反斜杠转义):

tweet_text = tweet_text.replace(/(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi, '<a href="$1">$1</a>');

And the Google Chrome (V8?) error is as follows: Google Chrome(V8?)错误如下:

Uncaught SyntaxError: Invalid regular expression: /(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/: Invalid group

And the Safari error is as follows: Safari错误如下:

SyntaxError: Invalid regular expression: unrecognized character after (?

He claims it should work in modern JavaScript regexp interpreters, which I'd assume WebKit & V8 would be. 他声称它应该适用于现代JavaScript regexp解释器,我假设它是WebKit和V8。 Does JavaScript's regexp syntax not support the (?: (damn Google for not indexing punctuation!) grouping syntax? Did I just miss escaping something? JavaScript的正则表达式语法是不是支持(?:该死的谷歌没有索引标点符号!)分组语法?我只是错过了逃避的东西吗?

Gah, it was the mode modifier (ie the (?i) ) at the beginning of the regex! Gah,它是正则表达式开头的模式修饰符 (即(?i) )!

I went through Regular-Expressions.info's datails on "JavaScript's Regular Expression Flavor" , specifically the list of what's not supported, and there was the 'mode modifier', which I had already specified after the closing forward slash of the regex. 我查看了Regular-Expressions.info关于“JavaScript的正则表达式风格”的数据 ,特别是支持的列表,还有“模式修饰符”,我已经在正则表达式的正式结束斜线之后指定了。 Ripped it out an all seems well. 把它撕成一片似乎很好。

So, my JavaScript regex is now as follows: 所以,我的JavaScript正则表达式现在如下:

/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM