Home >Web Front-end >JS Tutorial >JavaScript method to achieve global matching and replacement_javascript skills
The replace function is used to replace strings in JavaScript, but during actual use, it is found that this function will only replace the first matched character, which is very uncomfortable. In the PHP language, replace can Implement global matching and replacement. No way, after careful study, I found that there are other ways to achieve global matching and replacement.
(1) In fact, replace itself can also achieve this function, but it needs to add a parameter g in a regular form, for example:
or:
Replace all www.baidu.com in str characters with www.jb51.net
(2) Expand the js function library yourself and create your own function replaceall method to achieve global matching and replacement. As follows:
This actually also uses the idea of method one. An example is as follows (the same function as above is also implemented here, but it is more intuitive than method one):
The above is the entire content of this article, I hope you all like it.