Regex.Replace高级替换
一、对匹配的值进行复杂替换
string words = "如果您的计算机或网络受到防火墙,如果或者代理服务器的保护,请确认 Firefox 已被授权访问网络"; string pipeiStr = "网络"; string patter = @"("+pipeiStr+"+)";//正则表达式 MatchEvaluator matchEvaluator = new MatchEvaluator(MatchStrRed);//委托 string result = Regex.Replace(words, patter, matchEvaluator); //正则替换,每次匹配都会调用MatchStrRed函数 ViewBag.Result = result;
/// <summary> /// 把匹配的字符串变红 /// </summary> /// <param name="match"></param> /// <returns></returns> public static string MatchStrRed(Match match) { string result =match.Groups[0].Value; return "<span style='color:red;'>" + result + "</span>"; }

浙公网安备 33010602011771号