How can I replace all the javascript string replacement functions at a time?
JS string replacement function: Replace ("string 1", "string 2 ″)
1. we all know that the string replacement function in JS is Replace ("string 1", "string 2"), but this function can only Replace string 1 that appears for the first time, so how can we replace them all at once?
<Script> var s = "love life! Love java... "; alert (s); alert (s. replace ("LOVE", "LOVE"); alert (s. replace (/\ LOVE/g, "LOVE"); </script>
Save the above Code to an HTML file and you will see the effect in the browser.
How is it? If you understand it, you don't have to read it. If you don't understand it, you can read it again:
In fact, we use the regular expression in JS. In/\ LOVE/g,/\ LOVE indicates that the string is to be found. What we are looking for is quotation marks and/g indicates the regular expression syntax, it indicates the meaning of all, and here it indicates replacing all.
So the code above means to remove all the quotation marks in the string.
2. Now we know how to replace all strings, but how can we implement it if we want to pass LOVE as a parameter into a regular expression?
So let's take a look at how the following 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 is a tutorial on how to replace all the JS string replacement functions at a time. I hope you will like it.