跳到主要內容

[javascript] 一次全部字串取代(replace)

js的replace只會取代一次,非常不好用

 

用下列很妙的函式吧: replaceAll ( "<p><p><p><p><p><p>"  ,  "<p>" ,  "<br>" )

就能一次把<p>全部換成<br>

 

function replaceAll(txt, replace, with_this) {
return txt.replace(new RegExp(replace, 'g'),with_this);
}

留言