Home  >  Article  >  Web Front-end  >  Parameter usage when the second parameter of javascript replace() is a function (detailed explanation for everyone)

Parameter usage when the second parameter of javascript replace() is a function (detailed explanation for everyone)

亚连
亚连Original
2018-05-19 13:45:432429browse

The replace() function has a replacement function. It can have two parameters. The first parameter can be the string to be replaced or a regular expression matching the string to be replaced. The second parameter can be the replacement text. Or a function, let’s take a look at a few code examples about the replace() function

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

Thereplace() function has replacement Function, it can have two parameters. The first parameter can be the string to be replaced or a regular expression matching the string to be replaced. The second parameter can be the replacement text or a function. Let’s take a look at replace Several code examples of the () function.
Code example:
Example 1:

<script>
var str="I love jb51 and you?";
console.log(str.replace("jb","java"));
</script>

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

<script>
var str="I love jb51 and you?";
var reg=/jb/g;
console.log(str.replace(reg,"java"));
</script>

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

<script>
var str="I love jb51 and you?";
console.log(str.replace("jb",function(){
 return "java"}
));
</script>

In the above code, the second parameter is a function, and the return value of this function can be used to replace the specified string substring. When the second parameter is a function, the function can actually pass parameters. Let's introduce the parameter issues of the function through code examples.
The code is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>脚本之家</title>
<script type="text/javascript">
var url = "http://www.jb51.net/o.php?mod=viewthread&tid=14743&extra=page%3D1";
//第一参数为字符串
console.group("字符串");
var oneResult = url.replace("www.jb51.net",function(){
 console.log("replace输入参数:%o",arguments);
 var val = /www.jb51.net/.exec(url);
 
 console.log("exec输出参数:%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返回字符串:"+oneResult);
console.groupEnd("字符串");
    
//第一参数为正则表达式
console.group("正则表达式");
var regexp_global = /[?&](\w+)=([^&]*)/g;
var count = 0;
var twoResult = url.replace(regexp_global,function(){
 console.log("第"+(count++)+"次运行");
 console.log("replace输入参数:%o",arguments);
 var val = regexp_global.exec(url);
 console.log("exec输出参数:%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"]);
 return count;
});
console.log("replace返回字符串:"+twoResult);
console.groupEnd("正则表达式");
</script>
</head>
<body>
 
</body>
</html>

In the above code, it is demonstrated that the first parameter of the replace() function is an ordinary string and a regular expression. When the second function parameter passes parameters, a brief explanation will be given below:
The first parameter is an ordinary string:

When the first parameter is an ordinary string, Then only the first substring in the original string will be replaced, that is to say, only one replacement operation will be performed, for the parameters passed to the function and the array returned by the exec() function using ordinary string parameters as regular expressions. The elements are the same.
The first parameter is a regular expression:

Due to space reasons, only part of the running results are intercepted here. The first parameter of the replace() function is a regular expression, and the execution is global. match, then the second function parameter will be called multiple times, and the parameters passed each time are the same as the element contents of the array returned by regexp_global.exec(url).

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Use js to get the week of the year today is (small example, with source code)

Talk about HTML CSS JS (detailed explanation)

About how to optimize your JS code (graphic tutorial)

The above is the detailed content of Parameter usage when the second parameter of javascript replace() is a function (detailed explanation for everyone). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn