js replace函数输入参数

 

    测试了下replace函数的输入参数,

    之前一直不了解,如果replace第二个参数为function,那么replace函数提供给function的输入参数是什么呢?

    例如:

String.replace(regexp,function(?,?){})

    猜想是:

           function的输入参数跟exec函数返回结果想似。

 

先看测试代码:

<!DOCTYPE HTML>
<html>
  <head>
    <title>replace.html</title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
  </head>
  <body>
    <script type="text/javascript">
    	var url = "http://www.baidu.com/abc.jsp?method=method&name=abc&age=12";
    	
    	//第一参数为字符串
    	console.group("字符串");
    	var newUrl1 = url.replace("www.baidu.com",function(){
				    		console.log("replace输入参数:%o",arguments);
				    		var val = /www.baidu.com/.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 "123";
				    	});
	console.log("replace返回字符串:"+newUrl1);
    	console.groupEnd("字符串");
    	
    	//第一参数为正则表达式
    	console.group("正则表达式");
    	var regexp_global = /[?&](\w+)=([^&]*)/g;
    	var count = 0;
    	var newUrl2 = 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返回字符串:"+newUrl2);
	console.groupEnd("正则表达式");
    </script>
  </body>
</html>

 

firefox下执行结果:



 

 

    测试代码的断言都通过:

 

    1、显然replace第二个参数function的输入参数跟第一参数.exec(url)返回的信息一致。

    2、假如第一个参数不是正则表达式,function的输入参数依然为/第一参数/.exec(url)返回结果。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个使用正则表达式提取网址请求参数JavaScript函数示例: ```javascript function getQueryParamValue(url, key) { const regex = new RegExp(`[?&]${key}=([^&#]*)`); const match = regex.exec(url); return match ? decodeURIComponent(match[1].replace(/\+/g, ' ')) : null; } ``` 这个函数接受两个参数:`url`是包含请求参数的网址,`key`是要提取的请求参数的名称。函数使用正则表达式来匹配以`?`或`&`开头,后跟参数名称和参数值的字符串,并从提取参数值。 函数返回提取的参数值,如果参数不存在,则返回`null`。请注意,返回值是经过URL解码的,因为在URL参数值可能会进行编码。 以下是一个使用示例: ```javascript const url = 'https://example.com/?foo=bar&baz=qux'; const value = getQueryParamValue(url, 'foo'); console.log(value); // 输出: "bar" ``` 在这个例子函数将提取URL`foo`参数的值`bar`。 ### 回答2: 以下是一个用 JavaScript 编写的函数示例: ```javascript function getParamValue(url, key) { // 使用正则表达式提取参数部分 const params = url.match(/\?(.+)/)[1]; // 将参数字符串分割成键值对数组 const paramPairs = params.split('&'); // 遍历键值对数组,查找匹配的键,并返回对应的值 for (let pair of paramPairs) { const [paramKey, paramValue] = pair.split('='); if (paramKey === key) { return paramValue; } } // 如果找不到匹配的键,则返回 undefined return undefined; } // 示例用法 const url = "https://www.example.com/?name=John&age=25"; const key = "name"; const value = getParamValue(url, key); console.log(value); // 输出 "John" ``` 这个函数通过正则表达式提取出网址参数部分,然后将参数字符串分割成键值对数组。函数会遍历数组,查找与输入的键相匹配的键值对,并返回对应的值。如果找不到匹配的键,则返回 undefined。在示例函数被调用后会返回 URL 键为 "name" 的值,即 "John"。 ### 回答3: JavaScript可以使用正则表达式来提取网址请求参数。可以编写一个函数,接受一个参数作为key并返回对应的值。 下面是一个示例函数的实现: ```javascript function getParameterValue(url, key) { // 创建匹配URL参数正则表达式 const regex = new RegExp(`${key}=([^&]*)`); // 使用正则表达式匹配URL对应的参数值 const result = url.match(regex); // 如果匹配成功,则返回参数值 if (result && result.length >= 2) { return result[1]; } // 如果未找到对应的参数,则返回null或者可以自定义的默认值 return null; } ``` 使用这个函数,只需要将URL和对应的参数key作为参数即可: ```javascript const url = "https://www.example.com/?name=John&age=25"; const key = "age"; const value = getParameterValue(url, key); console.log(value); // 输出:25 ``` 这个函数会根据传的key在URL提取对应的参数值,并将其返回。如果未找到对应的参数,则返回null或者可以自定义的默认值。 需要注意的是,这个函数只能提取URL第一个匹配到的参数的值。如果需要提取多个参数,可以在函数做一些适当的调整,例如使用全局标志`g`来匹配到所有的参数值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值