JS string substitution function: replace ("String 1″," string 2″)
1. We all know that the string substitution function in JS is replace ("String 1″," string 2″), but this function can only replace the first occurrence of the string 1, so how can we replace all at once?
<script>
var s = "Love life!" Love JAVA ... ";
alert (s);
Alert ("Love", "S.replace");
Alert (S.replace (/\love/g, "Love"));
You can see the effect in your browser by saving the above code to an HTML file.
What do you think? If you understand the need to look down, do not understand and then look down:
In fact, we use the regular expression in JS,/\love/g/\love is to find the string, which we are looking for is the quotation marks,/g is the regular expression of the syntax, to express all the meaning, here is to represent the replacement.
So the above code means to remove all the quotes in the string.
2. Now we know how to replace all the strings, but what if we were to pass love as a parameter to the regular expression?
So let's look at how the following piece of code is implemented:
<script>
var s = "Love life!" Love JAVA ... ";
alert (s);
var tmp= "Love";
var reg=new RegExp ("");
Alert (S.replace (Reg, "Love"));
</script>[color=olive]
The above content is about JS string replacement function How to replace all the relevant tutorials, I hope you like.