先看一段代码

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>

相关文章:

  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-24
  • 2021-04-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
相关资源
相似解决方案