How can I replace all the javascript string replacement functions at a time?

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.