2010-06-28 6 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

감사합니다. – stighy

관련 문제