Data questa funzione:
function Repeater(template) {
var repeater = {
markup: template,
replace: function(pattern, value) {
this.markup = this.markup.replace(pattern, value);
}
};
return repeater;
};
Come faccio a this.markup.replace()sostituire globalmente? Ecco il problema. Se lo uso in questo modo:
alert(new Repeater("$TEST_ONE $TEST_ONE").replace("$TEST_ONE", "foobar").markup);
Il valore dell'avviso è "foobar $ TEST_ONE".
Se cambio Repeaterin quanto segue, non viene sostituito nulla in Chrome:
function Repeater(template) {
var repeater = {
markup: template,
replace: function(pattern, value) {
this.markup = this.markup.replace(new RegExp(pattern, "gm"), value);
}
};
return repeater;
};
... e l'avviso è $TEST_ONE $TEST_ONE.