Very strange result with regex in javascript
javascript, regex
Solution
You have to escape the delimiters ("/") using a backslash. The delimiters mark the begin and the end of the expression. You can only use the slash when you escape it with a backslash. The following expression should work:
var reg=/\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`!()\[\]{};:'".,<>?«»“”‘’]))/;
Problem
I am not very good in regex but there is an illogic thing that is happening : I verified the syntax of a regex with this fiddle : http://jsfiddle.net/BcQfQ/2/ and then replaced the `\d` to another regex to check an url from here : http://daringfireball.net/2010/07/improved_regex_for_matching_urls and it's not working : http://jsfiddle.net/bNHQs/2/. And the strangest thing is that when you copy the regex and paste it in a textbox (and then write textbox.value in the code), all is fine : http://jsfiddle.net/6uAQG/2/. Code of the not working regex : ``` var reg=/\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`!()\[\]{};:'".,<>?«»“”‘’]))/; var str="2"; if(str.match(reg))alert("test:true"); else alert("test:false"); ``` How can I write the regex in the code so that it works?