正则表达式中使用变量

项目里面有篇报告,本地存储了一个模板,里面数据是后台返回。本地模板里面的${}是需要后台替换的数据。例如:${count},后台会返回count的数量值。需要全局替换${count}为返回值。

本地有个map,循环map的key,替换所有结果。

for(let key in map){

  let str = '${' + key + '}';

  if(this.templateValue.indexOf(str) != -1){

    this.templateValue = this.templateValue.replace(new RegExp("\\${" + key + "}","g"),result[key]);

  }

}

刚开始使用的replaceAll,火狐不兼容。

new RegExp()可以设置第二个参数,如 i g。

posted on 2022-06-22 15:54  hanguahannibk  阅读(145)  评论(0)    收藏  举报