This article introduces some advanced use methods of Replace () in js. If you need it, you can refer to the following: in many projects, we often need to use JS, the replace () method of js is required to modify some elements in front of the page.
If you frequently use "ABCABCabc". replace ("A", "B"), you should be clear that the final result of the modified statement is BBCABC. This method can only be replaced
The first matched element. What if I replace all? Use a regular expression:
"ABCABCabc". replace (/A/g, "B.
What if I want to replace A with?
Then you can use "ABCABCabc". replace (/a/ig, "B ");
Flag: the size of the I-ID ignore is ignored, the g-ID global is repeatedly searched, and the m-ID multi-row retrieval (this is not tested yet)
You can also use their combination. For example, the ig used above replaces all the IDs and ignores the case.
Regular regular expression:
The Code is as follows:
Var reg = new RegExp (/patten/flag)
Var strs = "". match (reg );
When the flag uses g, strs returns a string array.
If you want to match any one of multiple strings, you can use
The Code is as follows:
Reg = new RegExp (/abc | xyz/ig );