Using the Replace method in JS to introduce _javascript skills

Source: Internet
Author: User
The replace () method replaces a string with some characters or replaces a substring that matches a regular expression.

It is to be noted that:If the regexp has global flag g when replaced with a regular expression, the replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

A simple example is described below:
Copy Code code as follows:

<script language= "JavaScript" >
var strm = "JavaScript is a good SC Ript language ";
//Here I want to replace the letter A with letter a
alert (Strm.replace ("A", "a"));

</script>
//result, it replaces only the first letter. But if you add regular expressions, the results are different! Replace () supports regular expressions, which match characters or strings according to the rules of regular expressions, and then give replacements!
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
//Here I want to replace the letter A with letter a
alert (Strm.replace (/a/, "a"));
</script>
//But the result has not changed, just a little modification is OK.

<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
//This replaces the letter a all with the letter A, and when the regular expression has a "G" flag, the representative handles the entire string
Alert (Strm.replace (/a/g, "a"));
</script>

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.