鉴于这一功能:JavaScript的替换/正则表达式
function Repeater(template) {
var repeater = {
markup: template,
replace: function(pattern, value) {
this.markup = this.markup.replace(pattern, value);
}
};
return repeater;
};
我如何this.markup.replace()
全球更换?这是问题。如果我这样使用它:
alert(new Repeater("$TEST_ONE $TEST_ONE").replace("$TEST_ONE", "foobar").markup);
警报的值是“foobar $ TEST_ONE”。
如果我改变Repeater
到以下内容,然后在没有在Chrome取代:
function Repeater(template) {
var repeater = {
markup: template,
replace: function(pattern, value) {
this.markup = this.markup.replace(new RegExp(pattern, "gm"), value);
}
};
return repeater;
};
...和警报$TEST_ONE $TEST_ONE
。
但替换()接收的正则表达式作为变量。 – core 2009-07-22 01:05:38
小修正 - `$`表示正则表达式中的行尾。 – harto 2009-07-22 01:12:19