Posted By

naren1012 on 02/10/11


Tagged

regex url javascript matcher


Versions (?)

Modified URL Matcher Regex


 / Published in: JavaScript
 

Modified regex of URL Matcher found on http://daringfireball.net/2010/07/improvedregexformatchingurls

  1. <html>
  2. <head>
  3. <title>URL Matcher Regex</title>
  4. <script>
  5.  
  6. function url_matcher($text)
  7. {
  8. //Original regex on http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  9. //(?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`!()\[\]{};:'".,<>?������«������»���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½���¢�¯�¿�½�¯�¿�½]))
  10.  
  11. //Modified regex
  12. 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,
  13. "<a href='" + "$1" + "'>" + "$1" + "</a>");
  14. return text;
  15. }
  16.  
  17. function start()
  18. {
  19. $text = "This regex is a modified version of the regex on http://daringfireball.net/2010/07/improved_regex_for_matching_urls <br/>";
  20. $text = $text + "This regex does not match the ellipsis as part of URL http://google.com...<== note that ellipsis not_part_of_link. <br/>"
  21. $text = $text + "Does not match bit.ly/.jee but matches something.something/something.something bit.ly/j.e <br/>"
  22. res = url_matcher($text);
  23. document.write('<b>Original Text :</b> <br/>');
  24. document.write($text);
  25. document.write('<br/><b>Clickified Text : </b><br/>');
  26. document.write(res);
  27. }
  28.  
  29. </script>
  30. </head>
  31.  
  32. <body onload='start()'>
  33.  
  34. </body>
  35. </html>

Report this snippet  

You need to login to post a comment.