JS keyword discoloration detailed many friends would like to achieve it. Next we will perform the following steps: 1. replace the keyword to color the font. use regular expressions to change the CSS background color. This method can be called in combination with the front-end JS. If you are interested, refer to it and hope to help you.
1. Replace the keyword to color the font
The Code is as follows:
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 that when the dot character is a case-sensitive English character, it is replaced with the keyword case after the color changes, and the experience is poor.
2. Use regular expressions to change the color of CSS background.
The Code is as follows:
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 + ""; // background color of the keyword
// Return "" + m. Value + ""; // the color of the keyword.
}
This method can be called in combination with foreground JS:
The Code is as follows:
The Code is as follows: