12

I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:

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

Anyone know how to solve this?

3
  • I think you need to also provide information on what the expression is supposed to do (mean) that way it's easier to check. Commented Aug 3, 2011 at 14:05
  • 1
    @Issun: It's this pattern: daringfireball.net/2010/07/improved_regex_for_matching_urls Commented Aug 3, 2011 at 14:15
  • This regex does not work for sites which does not start with www Commented May 25, 2017 at 14:52

3 Answers 3

23

As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).

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

I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)

Note: Whether you use a literal or a string, it has to be on 1 line.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for moving the i modifier to the end. JavaScript does not support the inline-modifier syntax ((?i)) that Gruber used.
@AlanMoore: Heh, I didn't actually think about that when I wrote it. I just moved it to the end, because it's javascript, so that's "where it belongs, darn it!". Of course, you're right that that's the reason, but I just did it as a reflex :)
0

put the whole expression in one line, and remove the quotes at the start and end so it looks like this var patt = /the-long-patttern/;, netbeans will still complain, but the browsers won't and thats what matters.

Comments

0

You should write it like this in NetBeans:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.