admin管理员组文章数量:1431998
I'm trying to replace multiple words with new words, each time I'm rendering an HTML template.
Instead of having to loop through the template (which is quite big) three times to look for three different words, I'd like to bine the three words and their replacements, and only loop through the template once. (Also, obviously below code only replaces the last word, {id} since it overrides the other two replacement attempts above it).
$.get('/templates/list-item.html', function(data) {
var template = data,
tmp = '';
$.getJSON('conf.json', function(data) {
var items = [],
list = data.default;
for (var item in list) {
var name = item.name,
value = list[item].value,
id = list[item].id;
tmp = template.replace('{name}', name);
tmp = template.replace('{value}', value);
tmp = template.replace('{id}', id);
items.push(tmp);
}
$('<div/>', {
html: items.join('')
}).appendTo('body');
});
});
Obviously rendering templates shouldn't be done with JS, but since it's for internal use only, no backend is available and it doesn't need to be indexed by Google, it's fine for the time being.
I'm trying to replace multiple words with new words, each time I'm rendering an HTML template.
Instead of having to loop through the template (which is quite big) three times to look for three different words, I'd like to bine the three words and their replacements, and only loop through the template once. (Also, obviously below code only replaces the last word, {id} since it overrides the other two replacement attempts above it).
$.get('/templates/list-item.html', function(data) {
var template = data,
tmp = '';
$.getJSON('conf.json', function(data) {
var items = [],
list = data.default;
for (var item in list) {
var name = item.name,
value = list[item].value,
id = list[item].id;
tmp = template.replace('{name}', name);
tmp = template.replace('{value}', value);
tmp = template.replace('{id}', id);
items.push(tmp);
}
$('<div/>', {
html: items.join('')
}).appendTo('body');
});
});
Obviously rendering templates shouldn't be done with JS, but since it's for internal use only, no backend is available and it doesn't need to be indexed by Google, it's fine for the time being.
Share Improve this question edited May 30, 2012 at 6:38 patad asked May 30, 2012 at 6:31 patadpatad 9,69211 gold badges42 silver badges44 bronze badges1 Answer
Reset to default 9You can use a callback function to replace template variables. Consider for example:
template = "{foo} and {bar}"
data = {foo:123, bar:456}
template.replace(/{(\w+)}/g, function($0, $1) {
return data[$1];
});
I'd also suggest replacing the loop with map():
items = $.map(list, function(item) {
var data = {name: item.name, value:.... etc }
return template.replace(/{(\w+)}/g, function($0, $1) {
return data[$1];
});
}
/{(\w+)}/g
basically means the following:
/ - start pattern
{ - match { literally
( - begin group 1
\w - a "word" character (letter+digit+underscore)
+ - repeat once or more
) - end group 1
} - match } literally
/ - end pattern
g - match globally, i.e. all occurences
When the callback function is called, it gets the whole match as its first parameter and the value of the group 1 as the second. So when it sees {foobar}
, it calls callback("{foobar}", "foobar")
.
本文标签: jqueryJavascript regex to replace multiple words with new wordsStack Overflow
版权声明:本文标题:jquery - Javascript regex to replace multiple words with new words - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745564310a2663667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论