0

I have the following code:

var temp = '<p class="fasfasfasd">Type <strong>Markdown</strong> here. {:.fasfasfasd}</p>'+
                '<p class="fdfsdf">fdsfsdf {:.fdfsdf}</p>'
//this should match everything like {:*}
var re = /\{:(.*?)\}/;
console.log(temp);
temp = temp.replace(re, "", 'gm');
console.log(temp);

Here a running example: http://jsfiddle.net/AL8DN/

I want to delete all occurences of the regex re in the string temp. However the second match of the regex is never deleted. What did I wrong?

4

1 回答 1

3

来自 Mozilla 开发人员参考:

在 String.replace 方法中使用 flags 参数是非标准的。不使用此参数,而是使用带有相应标志的 RegExp 对象

因此,通过将其更改为:

var temp = '<p class="fasfasfasd">Type <strong>Markdown</strong> here. {:.fasfasfasd}</p>'+
                '<p class="fdfsdf">fdsfsdf {:.fdfsdf}</p>'
var re = /\{:(.*?)\}/gm;
console.log(temp);
temp = temp.replace(re, "");
console.log(temp);

它似乎工作正常。

于 2013-10-15T17:41:05.513 回答