Keyword highlight-js method

Source: Internet
Author: User

The search keywords such as Google and Baidu are highlighted. I have compared several of them, but it is still the best in my hometown. The JavaScript method is very easy to implement. If you want to add it, you can check it out.

1. highlight a single keyword
Code:
<Div id = "TXT">
Use jsArticleHighlight the specified keyword in the content
Yes ..
These keywords: beauty, life, and shopping
When these keywords appear in the article, highlight them ..
The article generates static pages and these keywords can be updated at any time, so I want to use js to implement them...
</Div>
<Script language = "JavaScript">
Document. getelementbyid ("TXT "). innerhtml = document. getelementbyid ("TXT ").. innerhtml. replace (/article/GI, "<font color = Red> Article </font> ");
</SCRIPT>

2. Highlight multiple keywords
Code:
<Div id = "TXT">
Use js to highlight the specified keyword in the article content
Well, there are now these keywords: beauty, life, and shopping.
When these keywords appear in the article, highlight them ..
The article generates static pages, and these keywords can be updated at any time, so I want to use js to implement it...
</Div>
<Script language = "JavaScript">
Document. getelementbyid ("TXT "). innerhtml = document. getelementbyid ("TXT "). innerhtml. replace (/(Article) | (keyword) | (function)/GI, "<font color = Red> $1 $2 $3 </font> ");
</SCRIPT>

Character meaning
$ (Jscript 5.5 or later)
$ & Specifies the part of stringobj that matches the entire pattern. (Jscript 5.5 or later)
$ 'Specifies the stringobj section before matching by $. (Jscript 5.5 or later)
$ 'Specifies the stringobj part after matching by $. (Jscript 5.5 or later)
$ N indicates the nth child matching captured. Here, n is the first digit in decimal format from 1 to 9. (Jscript 5.5 or later)
$ NN: the NN sub-match captured by NN. NN is a two-digit decimal number from 01 to 99. (Jscript 5.5 or later)
G, global pattern matching
I, case-insensitive matching

Note:
1> If You Want To obtain keywords by passing parameters, you only need to adjust the code as appropriate.
2> pay attention to the matching of the same keywords in your style. For example, if the keyword is "1", your style contains Height = 1, if this is the case, errors will occur. You need to debug these specific errors on your own.

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.