2010-06-28 135 views
2

任何人都知道如何在某些文本中「發現」超鏈接,並在使用asp.net(或javascript)的html超鏈接中轉換該超鏈接。 例如,如果用戶輸入這樣的文字:ASP.NET:在html超鏈接中轉換純超鏈接

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

我怎樣才能發現和HTML轉換,如:

You found it at <a href='http://www.foo.com'>http....</a>

? 在此先感謝

回答

3

您應該可以很容易地使用正則表達式。

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); 
} 

正則表達式取自於此; http://www.geekzilla.co.uk/View2D3B0109-C1B2-4B4E-BFFD-E8088CBC85FD.htm

基於此示例使用MatchEvaluator; http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator(v=VS.71).aspx

+0

謝謝你mcatackney! – stighy 2010-06-28 10:02:51