The replace () character in JavaScript is replaced with a detailed explanation

Source: Internet
Author: User

The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.

Grammar
Stringobject.replace (regexp/substr,replacement)

return value
A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.

Description
The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.

Instance

The code is as follows Copy Code

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

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!

The code is as follows Copy Code

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

This only replaces the first letter A.

The code is as follows Copy Code
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Replace the letter a all with the letter A
Alert (Strm.replace (/a/g, "a"));
</script>

It is clear from the above that when a regular expression has a "G" flag, the representative will process the entire string.

The code is as follows Copy Code
<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
Alert (Strm.replace (/JavaScript) s* (IS)/g, "$ fun. It $"));
</script>

Example 2

The code is as follows Copy Code

<script language= "JavaScript" >
var reg=new RegExp ("End Ancient", "G"); Create a regular RegExp object
var stringobj= "End ancient People's Republic, end ancient people";
var newstr=stringobj.replace (Reg, "China");
alert (NEWSTR);
</script>


Let's take a look at a simple example: capitalize all the first letters of the word.

The code is as follows Copy Code

<script language= "JavaScript" >
var strm = "JavaScript is a good script language";
function Change (Word)
{
Return Word.indexof (0). toUpperCase () +word.substring (1);
}
Alert (Strm.replace (/bw+b/g,change));
</script>

It is clear from the above that when a regular expression has a "G" flag, the representative will handle the entire string, that is, the transformation of the function change will be applied to all matching objects. The function has three or more parameters, depending on the regular expression.

With functions and regular expressions, the replace () processing string has an unprecedented power!

Finally, for example, it is so easy to reverse all the words in a string, using the replace () process.

  code is as follows copy code
<script language= ' JavaScript '
var strm = ' JavaScript is a good script language ';
function Change (word)
{
var r Esult = Word.match (/(w)/g);
if (result)
{
var str = ';
for (var i=result.length-1; i>=0; i--)
{
str + = result;
}
return str;
}
Else
{
return ' null ';
}
}
Alert (Strm.replace/b (w) +b/g,change);
</script>

Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function rather than a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.

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.