复制代码 代码如下:
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " " + redkey + " ");
return strtitle;
}
该方法缺点是:点字符是含大小写的英文时,变色后统一替换为了关键字的大小写,体验不好。
2.用正则,CSS背景变色
复制代码 代码如下:
protected string HighlightText(string inputText,string searchWord)
{
System.Text.Regularexpressions.Regex expression = new System.Text.Regularexpressions.Regex(searchWord.Replace(" ", "|"), System.Text.Regularexpressions.RegexOptions.IgnoreCase);
return expression.Replace(inputText,new System.Text.Regularexpressions.Matchevaluator(ReplaceKeywords));
}
public string ReplaceKeywords(System.Text.Regularexpressions.Match m)
{
return "" + m.Value + "";//关键字背景加色
//return "" + m.Value + "";//关键字变色
}
该方法可结合前台JS调用:
复制代码 代码如下:
.highlightTxtSearch
{
background-color:Yellow;
}
复制代码 代码如下:



