/ Published in: JavaScript
Modified regex of URL Matcher found on http://daringfireball.net/2010/07/improved_regex_for_matching_urls
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<html> <head> <title>URL Matcher Regex</title> <script> function url_matcher($text) { //Original regex on http://daringfireball.net/2010/07/improved_regex_for_matching_urls //(?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`!()\[\]{};:'".,<>?������«������»���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½])) //Modified regex text = text.replace(/\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>"); return text; } function start() { $text = "This regex is a modified version of the regex on http://daringfireball.net/2010/07/improved_regex_for_matching_urls <br/>"; $text = $text + "This regex does not match the ellipsis as part of URL http://google.com...<== note that ellipsis not_part_of_link. <br/>" $text = $text + "Does not match bit.ly/.jee but matches something.something/something.something bit.ly/j.e <br/>" res = url_matcher($text); document.write('<b>Original Text :</b> <br/>'); document.write($text); document.write('<br/><b>Clickified Text : </b><br/>'); document.write(res); } </script> </head> <body onload='start()'> </body> </html>