Home >Web Front-end >JS Tutorial >Introduction to advanced usage of JS Replace()_javascript skills
In many projects, we often need to use JS to modify certain elements in the foreground in front of the page. The replace() method of js is essential.
Students who often use "ABCABCabc".replace("A","B") should know better. The final result of changing the statement is BBCABC. This method can only replace
The first matching element. What if you replace everything? Just use regular expressions:
"ABCABCabc".replace(/A/g,"B") will do.
What if you want to replace A and also replace a?
Then you can use "ABCABCabc".replace(/a/ig,"B");
Flag: i indicates ignore to ignore the size, g indicates global for repeated retrieval, and m indicates multi-line retrieval (this has not been tested yet)
You can also use a combination of them. For example, the ig used above replaces all identifiers and ignores case.
Formal regex writing:
If you want to match any one of multiple strings, you can use