【Javascript】实现字符串的replaceAll方法

Js中string对象只有replace方法,只能替换内容一次,那么就来实现一下replaceAll的方法

 

String.prototype.replaceAll= function (s1,s2){
return this .replace( new RegExp(s1, "gm" ),s2);
}

具体使用就很简单了 <wbr style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left; background-color:rgb(221,221,221)"><div style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left; background-color:rgb(221,221,221)"> <pre class="prettyprint" style="padding:2px; border:1px solid rgb(136,136,136)"><p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"> </p><div style="line-height:14px; margin:0px!important; padding:0px 1em!important; background-color:white!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace; font-size:13px; direction:ltr!important; color:rgb(85,85,85)"> <code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-weight:bold!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:rgb(0,102,153)!important"><span class="kwd" style="line-height:23px; color:rgb(0,0,136)">var</span></code><span class="pln" style="line-height:23px; color:rgb(0,0,0)"> </span><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pln" style="line-height:23px">str</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">=</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:blue!important"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"abcabcabc"</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">;</span></code> </div><div style="line-height:14px; margin:0px!important; padding:0px 1em!important; background-color:white!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace; font-size:13px; direction:ltr!important; color:rgb(85,85,85)"> <code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pln" style="line-height:23px">alert</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:23px">str</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:23px">replace</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:blue!important"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"a"</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:23px"> </span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:blue!important"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"*"</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">));</span></code> </div><div style="line-height:14px; margin:0px!important; padding:0px 1em!important; background-color:white!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace; font-size:13px; direction:ltr!important; color:rgb(85,85,85)"> <code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pln" style="line-height:23px">alert</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span><span class="pln" style="line-height:23px">str</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">.</span><span class="pln" style="line-height:23px">replaceAll</span><span class="pun" style="line-height:23px; color:rgb(102,102,0)">(</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:blue!important"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"a"</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">,</span><span class="pln" style="line-height:23px"> </span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:blue!important"><span class="str" style="line-height:23px; color:rgb(0,136,0)">"*"</span></code><code style="line-height:1.1em!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,'Bitstream Vera Sans Mono','Courier New',Courier,monospace!important; font-size:1em!important; direction:ltr!important; display:inline!important; color:black!important"><span class="pun" style="line-height:23px; color:rgb(102,102,0)">));</span></code> </div> <p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px"></p></pre> 转载于<a target="_blank" rel="nofollow" href="http://witmax.cn/" style="color:rgb(81,131,217); text-decoration:none">枫芸志</a> </div> <div style="line-height:25px; color:rgb(51,51,51); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px; text-align:left; background-color:rgb(221,221,221)"> 原文地址<a target="_blank" rel="nofollow" href="http://witmax.cn/js-replaceall.html" style="color:rgb(81,131,217); text-decoration:none">witmax.cn/js-replaceall.html</a> </div> </wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值