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:
<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"]);
Console.log ("Replace return string:" +oneresult);
Console.groupend ("string");
The first parameter is a regular expression
Console.group ("Regular expression");
Varregexp_global =/[?&] (\w+) = ([^&]*)/g;
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"]);
Console.log ("Replace return string:" +tworesult);
Console.groupend ("Regular expression");
|
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.