Javascript 将特定单词替换为标记

Javascript 将特定单词替换为标记,javascript,Javascript,下面有一个字符串,在大括号内有一些单词,我想提取单词并将其作为数组 输入: let string = 'Hello John \n, the time is ${time} and its ${day} of ${month} ${year}' let keys = ["time", "day", "month", "year"] 输出: let string = 'Hello John \n, the ti

下面有一个字符串,在大括号内有一些单词,我想提取单词并将其作为数组

输入

 let string = 'Hello John \n, the time is ${time} and its ${day} of ${month} ${year}'
let keys = ["time", "day", "month", "year"]
输出

 let string = 'Hello John \n, the time is ${time} and its ${day} of ${month} ${year}'
let keys = ["time", "day", "month", "year"]
在正则表达式中实现它的正确方法是什么

如何用输入标记替换字符串,如下所示

  let string = 'Hello John \n, the time is <input name='time' /> and its <input name='day' /> of <input name='month' /> <input name='year' />'
let string='Hello John\n,时间是

您可以捕获所有匹配的组

const string='Hello John\n,时间是${time},它的${day}是${month}${year}'
常量regex=/\$\{(\w+)\}/g
常数res=[];
让火柴
while(匹配项=regex.exec(字符串)){
res.push(匹配[1]);
}

console.log(res)
Tip:可以将函数作为替换参数。@tadman但replace可以。replace('text','newText'),但如何使用函数而不是字符串来.replace('${?}')动态内容。看看MDN示例。有一些是这样的。