1. Replace keywords and change the font color
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " " redkey " ");
return strtitle;
}
The disadvantage of this method is: when the dot character is English with upper and lower case, it will be replaced with the upper and lower case of the keyword after changing color, which is not a good experience.
2. Use regular expressions to change the CSS background color
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 "";//Keyword background color
//return "" m.Value "";//Keyword color change
}
This method can be combined with the frontend JS call :