0

i found a regex that look for matching url here : http://daringfireball.net/2010/07/improved_regex_for_matching_urls, the regex is :

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

but when i try to use it in my js file firebug shows me SyntaxError: syntax error exactly in this part (?i)

i use the regex like the following :

text = $('textarea').val();
url_regex = (?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]));
match = url_regex.exec(text);

where is the problem please ?

8
  • Did you copy and paste that line directly into your js, or is there some more code around it? Commented Nov 16, 2013 at 3:39
  • stackoverflow.com/questions/15145659/… explains (?i) turns off case sensitivity (?-1) turns case sensitivity back on. Commented Nov 16, 2013 at 3:40
  • i update my question, i copy and paste the line without modification, then assign it to url_regex variable Commented Nov 16, 2013 at 3:41
  • You need to add delimiters to your pattern. Wrap it in / characters to tell Javascript it's a regex. Commented Nov 16, 2013 at 3:41
  • Your regex needs to be fenced by /../ -- see developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Nov 16, 2013 at 3:41

2 Answers 2

2

In javascript, regular expressions are wrapped in /s. Also, the (?i) part needs to be replaced with a i at the end to make the regex case insensitive.

text = $('textarea').val();
url_regex=/\b((?:https?:\/\/|(www\d{0,3}[.])?|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i
match = url_regex.exec(text);

So, as others have said, there are some differences between regex in javascript and perl.

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

4 Comments

thank you @SuperScript it works now :) just one question, can i render "www" optional ?
there is some error in your comment code when i copy past it !! if i understand i need just change this part : (www\d{0,3}[.])? , but it doesn't work without www
I've edited it into my answer. Just re-copy the regex there.
now with this modification it works without www but problem if i put some text before url it doesn't work !! before changes it works even with text first
1

Regular expression needs to be wrapped in forward slashes and all forward slashes in it needs to be escaped with a back slash. Second (?i) is a flag to turn on case insensitive mode I guess it's for Perl but not supported in Javascript in the same way.

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

Should work. Also if you want to turn case insensitive mode you should use like this:

new RegExp('.*', 'i');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.