Find url in plain text and insert HTML A markups

我只是一个虾纸丫 提交于 2019-12-01 00:22:44
nhu

You can use a regex for this. If you need a better Regex you can search for it here http://regexlib.com/Search.aspx?k=url

My quick solution for this would be this:

string mystring = "My text and url http://www.google.com The end.";

Regex urlRx = new Regex(@"(?<url>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)", RegexOptions.IgnoreCase);

MatchCollection matches = urlRx.Matches(mystring);

foreach (Match match in matches)
{
    var url = match.Groups["url"].Value;
    mystring = mystring.Replace(url, string.Format("<a href=\"{0}\">{0}</a>", url));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!