1 Regex urlregex = new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?", // 正则表达式 2 RegexOptions.IgnoreCase | RegexOptions.Compiled); 3 4 5 string oldProName = modelProName; 6 string newProName = modelProName; 7 8 string strtitle = modelProName; 9 10 if (urlregex.IsMatch(modelProName)) 11 { 12 foreach (Match match in urlregex.Matches(modelProName)) //Match对象 13 { 14 string url = match.Value; 15 if (url != match.NextMatch().Value) //防止重复替换相同的链接 16 { 17 // newProName = urlregex.Replace(oldProName, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">点击打开</a>"); 18 newProName = newProName.Replace(url, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">点击打开</a>"); //利用String.Replace(),将获取到的子字符串替换成replacement 19 } 20 } 21 22 23 }