How to fix an "invalid group" error when trying to use the "enhanced" Gruger URL for regexp url template in JavaScript?

I am trying to integrate John Gruber's Improved Liberal, Accurate Regular Expression Pattern for matching URLs in one of my Javascripts, but the WebKit inspector (on Google Chrome 5.0.375.125 for Mac) gives the regular expression syntax error "Invalid group".

The original Gruber regex is as follows:

(?i)\b((?:[az][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][az]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?ยซยป""''])) 

A line from my regex JavaScript is as follows: (w / forward slashes backslash-escaped):

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

And the Google Chrome bug (V8?) Looks like this:

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

And the Safari error looks like this:

 SyntaxError: Invalid regular expression: unrecognized character after (? 

He claims that he should work in modern regexp JavaScript interpreters, which I would suggest that WebKit and V8 will. JavaScript syntax regexp does not support grouping syntax (?: (Damn google for not indexing punctuation!)? I just missed something?

+7
javascript regex
source share
1 answer

Gah, that was the mode modifier (i.e. (?i) ) at the beginning of the regular expression!

I looked at the Regular-Expressions.info datails in the "JavaScript Regular Expression Flavor" , in particular a list of what is not supported, and there was a "mode modifier" that I already specified after closing the slash of the regular expression. Sorry, everything looks good.

So, my JavaScript expression now looks like this:

 /\b((?:[az][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][az]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?ยซยป""'']))/gi 
+14
source share

All Articles