如何修复“无效群组”尝试使用Gruber的“改进”时出错JavaScript中的URL匹配regexp模式? [英] How do I fix "invalid group" error when attempting to use Gruber's "improved" URL matching regexp pattern in JavaScript?

查看:131
本文介绍了如何修复“无效群组”尝试使用Gruber的“改进”时出错JavaScript中的URL匹配regexp模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整合John Gruber的改进的自由,准确的正则表达式模式以匹配网址进入我的Javascripts之一,但WebKit的检查员(在谷歌Chrome 5.0.375.125 for Mac中)给出了无效组正则表达式语法错误。

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.

Gruber的原始正则表达式为如下:

Gruber's original regexp is as follows:

(?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`!()\[\]{};:'".,<>?«»""‘’]))

我的JavaScript与正则表达式的行如下(带正斜杠)反斜杠转义):

The line from my JavaScript w/the regexp is as follows (w/forward slashes backslash-escaped):

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

Google Chrome(V8?)错误如下:

And the Google Chrome (V8?) error is as follows:

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

Safari错误如下:

And the Safari error is as follows:

SyntaxError: Invalid regular expression: unrecognized character after (?

之后无法识别的字符

他声称它应该适用于现代JavaScript regexp解释器,我假设它是WebKit和V8。JavaScript的regexp语法是否不支持(?: (该死的Google没有为标点符号编制索引!)分组语法?我只是错了s逃脱了什么?

He claims it should work in modern JavaScript regexp interpreters, which I'd assume WebKit & V8 would be. Does JavaScript's regexp syntax not support the (?: (damn Google for not indexing punctuation!) grouping syntax? Did I just miss escaping something?

推荐答案

Gah,它是模式修饰符(即在正则表达式开头的(?i))!

Gah, it was the mode modifier (i.e. the (?i)) at the beginning of the regex!

我经历了 Regular-Expressions.info关于JavaScript正则表达式风味的数据,特别是不是支持,并且有'模式修饰符',我在正则表达式的结束正斜杠之后已经指定了它。把它全部撕掉了。

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. Ripped it out an all seems well.

所以,我的JavaScript正则表达式现在如下:

So, my JavaScript regex is now as follows:

/\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

这篇关于如何修复“无效群组”尝试使用Gruber的“改进”时出错JavaScript中的URL匹配regexp模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆