JavaScript Regular method Replace implements search keyword highlighting _ regular expression

Source: Internet
Author: User
Here is a description of JavaScriptThe Replace method of the regular expression, and the ability to implement the search keyword highlighting. First introduce the Replace method of the regular expression, the following article contains JavaScriptSource Code and JavaScriptSource explanation, together to see this article detailed it

Objective

Regular expressions are powerful tools in string processing tools. Some people think it is just a small toy, but it is inseparable from it anyway.

This is the Replace method for JavaScript regular expressions, and the ability to implement search keyword highlighting.

First introduce the Replace method of regular expression

Replace introduction

W3school Original Link Introduction

How regular expressions use special characters $ to represent the original text, which is the key to achieving the search highlighting,


var str = "Asad sad 123 sd Qwe21"; Str.replace (/\d+/img, "number");//Here the correct match to the number, and replaced by the Chinese number console.log (str);//"Asad sad digital SD Q We digital "------------------------------------------------------//See how to use $ $, which represents the captured string var str =" Asad sad 123 sd Qwe21 "; Str.replace (/\d+/img, "digital"); Console.log (str);//"Asad sad Digital Digital SD qwe Digital"//Obviously not successful, $ $ or $ $, then how to use it correctly?------ ------------------------------------------------var str = "Asad sad 123 sd Qwe21"; str.replace (/(\d+)/img, "digital");// This matches the number correctly and replaces Console.log (str) with the original string, or///"Asad sad Digital 123 Digital SD qwe number 21 Number"/* () in the regular representation of the capturing tuple, you can use the special characters to represent the replaced content, You can have multiple () tuples, that is, you can have multiple $1,$2 */


Start a little trial.


<! DOCTYPE html>

The above is a small series to introduce the JavaScript regular method replace to achieve the search keyword highlighting, we hope to help.

Related recommendations:

jquery implements a search box similar to Baidu

The method of implementing asynchronous real-time search in jquery with select plugin

JavaScript Regular method Replace implements search keyword highlighting

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.