Home > Web Front-end > JS Tutorial > body text

JS keyword discoloration implementation ideas and code_javascript skills

WBOY
Release: 2016-05-16 17:41:49
Original
1316 people have browsed it
1. Replace keywords and change the font color
Copy the code 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: 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
Copy the code 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 "";//Keyword background color
//return "" m.Value "";//Keyword color change
}

This method can be combined with the frontend JS call :
Copy code The code is as follows:



Copy code The code is as follows:


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template