在 Mustache.js 中循环遍历相同变量名称的不同值?
Cycling through different values for the same variable name in Mustache.js?
我有一些数据如下:
hello this is {{replacement_data}} and this {{replacement_data}} is {{replacement_data}}
。
我想使用 Mustache 将它们替换为一组值:
[val1, val2, val3
.
这种事情可能吗?目前,我正在 Java 中编写一个丑陋的循环,并希望用类似的东西清理它。
如果这就这么简单,您就不需要车把或小胡子了。使用 RegEx 和几行代码非常简单。请参见下面的示例。给定一个替换数组,按照与被替换内容相同的顺序,搜索每一个替换为对应的数组元素。
简单。
var replacements = ['first replacement', 'second replacement', 'third replacment'];
let subject = document.querySelector('div');
let input = subject.textContent;
let output = input.replace(/{{replace me}}/g, match=>replacements.shift());
subject.textContent = output;
<h3>Replacements below...</h3>
<div>
This is some string with {{replace me}} and {{replace me}} and {{replace me}}!
</div>
我有一些数据如下:
hello this is {{replacement_data}} and this {{replacement_data}} is {{replacement_data}}
。
我想使用 Mustache 将它们替换为一组值:
[val1, val2, val3
.
这种事情可能吗?目前,我正在 Java 中编写一个丑陋的循环,并希望用类似的东西清理它。
如果这就这么简单,您就不需要车把或小胡子了。使用 RegEx 和几行代码非常简单。请参见下面的示例。给定一个替换数组,按照与被替换内容相同的顺序,搜索每一个替换为对应的数组元素。
简单。
var replacements = ['first replacement', 'second replacement', 'third replacment'];
let subject = document.querySelector('div');
let input = subject.textContent;
let output = input.replace(/{{replace me}}/g, match=>replacements.shift());
subject.textContent = output;
<h3>Replacements below...</h3>
<div>
This is some string with {{replace me}} and {{replace me}} and {{replace me}}!
</div>