Javascript string replace with dynamically created pattern -


my code replaces first match, ignoring flags (global , multi line). doing wrong?

for (var = 0; < values.length; i++) {         template = template.replace('{' + + '}', values[i].tostring().trim(), 'gm');     } 

as can see, placeholders have format: {0}, {1} etc

according mdn, flags non-standard normal .replace() method. instead, can pass in regexp same result.

template.replace(new regexp('\\{' + + '\\}', 'gm'),     values[i].tostring().trim()); 

since curly braces have special significance in regular expressions, have escape them.


Comments