js中没有java中的replaceAll()函数,为了达到与java的replaceAll()一样的效果,我们可以用如下代码实现:<wbr></wbr><wbr></wbr>
<wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">String.prototype.replaceAll<wbr>=<wbr>function(s1,s2)<wbr>{<wbr></wbr></wbr></wbr></wbr></span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em"><wbr><wbr><wbr><wbr>return<wbr>this.replace(new<wbr>RegExp(s1,"gm"),s2);<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">}</span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">调用方式:</span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">如想替换字符串"aa<wbr>bb<wbr>cc"中的所有空格,可以执行"aa<wbr>bb<wbr>cc".replaceAll("<wbr>",<wbr>"");</wbr></wbr></wbr></wbr></wbr></wbr></span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">---------------------------------------------------------------------------------------------------</span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">还有一种效率比较低的方法</span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">while(<wbr>str.indexOf(<wbr>"oldstr"<wbr>)<wbr>!=<wbr>-1<wbr>)<wbr>{</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em"><wbr><wbr><wbr><wbr><wbr>str.replace("oldstr","newstr");<wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><wbr></wbr></wbr></strong><wbr><br><strong><wbr><span style="color:#000000; word-wrap:normal; word-break:normal; line-height:1.3em">}</span><wbr></wbr></wbr></strong><wbr><br><br><br> function init(){<br> var docid=document.getElementsByName("docid")[0].value;<br> var log=document.getElementByIdx_x_x("lc_test").value;<br><span style="word-wrap:normal; word-break:normal; color:rgb(255,0,0)">var temp=log.replace(new<wbr>RegExp("N. "+docid+" -<wbr>", "gm" ),"");</wbr></wbr></span><br style="color:rgb(255,0,0)"><span style="word-wrap:normal; word-break:normal; color:rgb(255,0,0)">alert(temp);</span><br> }<br><br><br> 原文地址:http://lorenzooz.blog.163.com/blog/static/174240531201084113645814<wbr>/</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>