Converting URLs Into Links With Regex
/// <summary> /// Finds web and email addresses in a string and surrounds then with the appropriate HTML anchor tags /// </summary> /// <param name="s"></param> /// <returns>String</returns> public static string WithActiveLinks(this string s) { //Finds URLs with no protocol var urlregex = new Regex(@"\b\({0,1}(?<url>(www|ftp)\.[^ ,""\s<)]*)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled); //Finds URLs with a protocol var httpurlregex = new Regex(@"\b\({0,1}(?<url>[^>](http://www\.|http://|https://|ftp://)[^,""\s<)]*)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled); //Finds email addresses var emailregex = new Regex(@"\b(?<mail>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled); s = urlregex.Replace(s, " <a href=\"http://${url}\" target=\"_blank\">${url}</a>"); s = httpurlregex.Replace(s, " <a href=\"${url}\" target=\"_blank\">${url}</a>"); s = emailregex.Replace(s, "<a href=\"mailto:${mail}\">${mail}</a>"); return s; }
This will convert most URLs, but not all. Parsing URLs is not the easiest thing to do so you need to make a judgement on what type of URLs your users/visitors are most likely to provide and alter the regex patterns accordingly. One thing to point out is that the second pattern (the one that matches URLs with a protocol - http, https etc) also checks to make sure that it isn't already a hyperlink. By the time the second Replace() operations takes place, URLs without protocols will already be fitted with them, and have HTML surrounding them.
Currently rated 4.36 by 11 people
Rate Now!
Date Posted:
22 May 2010 22:56
Last Updated:
Posted by:
Mikesdotnetting
Total Views to date:
5791



Comments
04 December 2010 20:02 from Tvrtko
Hello Mike,
this works great, however, part with email link will duplicate anchor tag. For http urls you did ignore existing html anchors from match correctly , but for email they get matched even if they have html anchor around already.