【发布时间】:2020-08-19 08:40:59
【问题描述】:
如何按照提供的数组顺序替换我有 #sometext# 的所有实例?
文本是一个字符串,我的目标是搜索所有有一些文本被“#”字符包围的实例。然后按照数组的顺序用数组中的值替换那些实例
例子:
const text = "You are the #RANKED# highest ranked user (out of #TOTALUSER# people)"
const myArray = [10, 50];
搜索替换后的文字应该是:
"You are the 10 highest ranked user (out of 50 people)"
【问题讨论】:
-
可以使用
const result = text.replace(/#([^#]+)#/g, _ => myArray.shift());,不想修改原版的可以先克隆myArray
标签: javascript regex string replace