views:

282

answers:

2

I have a ASP.Net page for user to post their comments. Now I want that once user posts comments if any hypelink is found in the posted text then how can we make the hyperlink clickable.

Right now we are displaying the text and hyperlikn is not clickable

+1  A: 

Use regular expressions to find hyperlink patterns. Then re-save the content with the html a tags.

EDIT: Here is an example to get you started, Run this as a console app to see whats going on:

class Program
    {
        static void Main(string[] args)
        {
            string s = "http://www.google.com is the best site, followed then by http://www.yahoo.com";

            string pattern = @"http(s)?://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);
            System.Text.RegularExpressions.MatchCollection matches = regex.Matches(s);
            for (int i = 0; i < matches.Count; i++)
            {
                Console.WriteLine(string.Format("<a href=\"{0}\">{1}</a>", matches[i].Value, matches[i].Value));                }



        }
    }

The regular expression pattern was taken from: http://weblogs.asp.net/farazshahkhan/archive/2008/08/09/regex-to-find-url-within-text-and-make-them-as-link.aspx

Can u please give code for reference
Bhavi
A: 

response.write("<a href=""" & rs("TheURLField") & """>The Link</a>")

hhh