views:

49

answers:

1

Hi folks, anyone know how to "discover" hyperlink in some text and convert that hyperlink in html hyperlink with asp.net (or javascript). For example, if a user enter this text:

You found it at http://www.foo.com

How can i found and convert in html like :

You found it at <a href='http://www.foo.com'&gt;http....&lt;/a&gt;

? Thanks in advance

+3  A: 

You should be able to use Regular Expressions for this easily enough.

string InsertHyperLinks(string input)
{
    string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)";
    Regex r = new Regex(pattern);
    MatchEvaluator myEvaluator = new MatchEvaluator(delegate(Match m) { return String.Format("<a href=\"{0}\">{0}</a>", m.ToString()); });
    return r.Replace(input, myEvaluator);
}

Regular expression taken from here; http://www.geekzilla.co.uk/View2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm

Use of MatchEvaluator based on this example; http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator(v=VS.71).aspx

C.McAtackney
thank you mcatackney !!
stighy