JS keyword color changing Implementation ideas and code _ javascript tips-js tutorial

Source: Internet
Author: User
Tags regex expression domian
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:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.