2 回答
TA贡献1811条经验 获得超4个赞
一种方法是使用正则表达式替换。按照代码:
run1.onclick = function() {
output1.innerHTML = input1.value.replace( /{{(.*?)}}/g, '${$1}');
}
<input id="input1" type="text" value="'Lorem ipsum dolor {{variable1}}, sit amet consectetur {{otherVaraible}} elit. Iure facilis quaerat obcaecati tempore {{variable2}} praesentium, nisi culpa vero'">
<input id="run1" type="button" value="run">
<br />
<span id="output1"></span>
TA贡献2021条经验 获得超8个赞
使用 regex101.com 生成的示例。你可以在这里
玩它
const regex = /{{(.*?)}}/gm;
const str = `Lorem ipsum dolor {{variable1}}, sit amet consectetur {{otherVaraible}} elit. Iure facilis quaerat obcaecati tempore {{variable2}} praesentium, nisi culpa vero`;
const subst = `${$1}`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
添加回答
举报