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>