Detailed JavaScript replace () parameter usage when the second parameter is a function

Source: Internet
Author: User

The second parameter of JavaScript's replace () is the parameter of the function:

The replace () function has a replacement function, it can have two parameters, the first argument can be a string to be replaced or a regular expression that matches the string to be replaced, the second argument can be a replacement text or a function, and then take a look at a few code instances of the replace () function.
Code instance:
Example one:

<script>
Varstr= "I love jb51";
Console.log (Str.replace ("JB", "Java"));
</script>

The above code can only replace the first specified substring in the string.
Example two:

<script>
Varstr= "I love jb51";
varreg=/jb/g;
Console.log (Str.replace (Reg, "Java"));
</script>

The code above can replace all the specified substrings in the string.
Example three:

<script>
Varstr= "I love jb51";
Console.log (Str.replace ("JB", function () {
Return "Java"}
));
</script>

In the code above, the second argument is a function that replaces the substring specified in the string with the return value of this function. When the second parameter is a function, in fact, this function can pass the parameter, the following is a code example to introduce the function of the parameter problem.
The code is as follows:

<! DOCTYPE html>
<meta charset= "Utf-8" >
<title> Script House </title>
<script type= "Text/javascript" >
varURL = "Http://www.jb51.net/o.php?mod=viewthread&tid=14743&extra=page%3D1";
The first parameter is a string
Console.group ("string");
Varoneresult = Url.replace ("Www.jb51.net", function () {
Console.log ("Replace input parameter:%o", arguments);
Varval =/www.jb51.net/.exec (URL);
Console.log ("exec output parameter:%o", Val);
Console.assert (arguments[0] = = = Val[0]);
Console.assert (arguments[1] = = = val["index"]);
Console.assert (arguments[2] = = = val["input"]);
return "JB51";
});
Console.log ("Replace return string:" +oneresult);
Console.groupend ("string");
The first parameter is a regular expression
Console.group ("Regular expression");
Varregexp_global =/[?&] (\w+) = ([^&]*)/g;
Varcount = 0;
Vartworesult = Url.replace (Regexp_global,function () {
Console.log ("First" + (count++) + "Run");
Console.log ("Replace input parameter:%o", arguments);
Varval = regexp_global.exec (URL);
Console.log ("exec output parameter:%o", Val);
Console.assert (arguments[0] = = = Val[0]);
Console.assert (arguments[1] = = = Val[1]);
Console.assert (arguments[2] = = = Val[2]);
Console.assert (arguments[3] = = = val["index"]);
Console.assert (arguments[4] = = = val["input"]);
Returncount;
});
Console.log ("Replace return string:" +tworesult);
Console.groupend ("Regular expression");
</script>
<body>
</body>

In the above code, the first argument of the replace () function is a normal string and regular expression, and the second function parameter passes the argument, the following is a simple description:
The first argument is a normal string:

When the first argument is a normal string, it replaces only the first substring in the original string, which means that only one substitution operation is performed, and the parameters passed by the function are the same as the elements of the array returned by the EXEC () function as a regular string parameter.
The first argument is a regular expression:

Because of space, this is just a section of the run results, the first parameter of the replace () function is a regular expression, and a global match is performed, then the second function argument is invoked multiple times, and the arguments passed at each call are also and regexp_global.exec The element contents of the array returned by the (URL) are the same.

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.