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