首页 > Web开发 > 详细

关于js replace 第二个参数时函数时,函数参数解析

时间:2019-04-10 15:56:17      阅读:118      评论:0      收藏:0      [点我收藏+]

先看一段代码

function formateString(str,obj) {
    return str.replace(/\{#(\w+)#\}/g,function(match,key,index,source){
        console.log(arguments);
        return obj[key]
    })
}
var string=‘<div>{#content#}</div>‘;
formateString(string,{content:‘helloWorld‘});

match 是匹配到字符串 示例中 为{#content#}

key 是捕获分组中内容(无分组时不存在),正则表达式中小括号内的内容为一个分组,所以示例中为content

index 是字符串的下表也就是示例中{的下标即5

source 是原字符串 示例中为<div>{#content#}</div>

关于js replace 第二个参数时函数时,函数参数解析

原文:https://www.cnblogs.com/yihuite-zch/p/10683486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!