W
Will
I have the following code:
var rx = /{{(.+?)}}/i;
var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this
{{string}} needs to be {{replaced}} with a different {{value}}.';
var values = [];
values["value"] = "foo";
values["string"] = "bar";
values["braces"] = "bundy";
while(expr.match(rx)) {
expr = expr.replace(rx, values[RegExp.$1]);
}
Can anyone suggest a more efficient way of writing the match/replace
loop. Is there any way to do it with a single replace() call only?
Please note that I'm looking solely at improving the efficiency of the
while() loop. The actual input string and array of values are
irrelevant and for demonstration purposes only.
TIA
Will
var rx = /{{(.+?)}}/i;
var expr = 'each {{word}} wrapped in {{curly}} {{braces}} in this
{{string}} needs to be {{replaced}} with a different {{value}}.';
var values = [];
values["value"] = "foo";
values["string"] = "bar";
values["braces"] = "bundy";
while(expr.match(rx)) {
expr = expr.replace(rx, values[RegExp.$1]);
}
Can anyone suggest a more efficient way of writing the match/replace
loop. Is there any way to do it with a single replace() call only?
Please note that I'm looking solely at improving the efficiency of the
while() loop. The actual input string and array of values are
irrelevant and for demonstration purposes only.
TIA
Will