- 浏览: 30712 次
文章分类
最新评论
【Javascript】实现字符串的replaceAll方法
Js中string对象只有replace方法,只能替换内容一次,那么就来实现一下replaceAll的方法
具体使用就很简单了<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>
String.prototype.replaceAll=
function
(s1,s2){
return
this
.replace(
new
RegExp(s1,
"gm"
),s2);
}
相关推荐
主要介绍了JavaScript实现的字符串replaceAll函数代码分享,本文直接给出实现代码,需要的朋友可以参考下
此外,Java 中还提供了许多其他的字符-字符串处理方法,例如 String 类的 `replaceAll()` 方法、`replaceFirst()` 方法、`split()` 方法等,这些方法可以实现各种字符串处理操作。 Java 中的字符-字符串常用方法为...
主要介绍了JavaScript使用replace函数替换字符串的方法,涉及javascript中replace函数的使用技巧,非常具有实用价值,需要的朋友可以参考下
替换 JavaScript 字符串中的所有实例。 使用 npm 安装 npm install replaceall 然后在您的节点应用程序中包含 replaceall: var replaceall = require("replaceall"); 使用替换 var result = replaceall ( ...
从中截取字符串.asp 从中截取字符串 切分字符串.asp 切分字符串 删除字符串前后导空格.asp 删除字符串前导空格.asp 删除字符串后导空格.asp 反向截取字符串.asp ...
目的是要实现一个非常快速的字符串替换功能,而不会出现基于RegExp的String.replace() 。 解决此问题的常用方法是不查找要查找的内容和要替换的内容,因为两者都处理特殊字符。 这种解决方案称为replaceAll()。 ...
苏蒂尔斯 ... // => "baa" ,它仅替换了指定的子字符串之一。 executeTemplateLiteral(string: string, args: any[], argNames: string[], replace$: boolean = false) executeTemplateLiteral ( "test ${
// 重复字符串 String.prototype.repeat = function(n) { return new Array(n+1).join(this); } // 替换全部 String.prototype.replaceAll = function(str1, str2) { return this.replace(new RegExp(str1, "gm"),...
某些其他 JavaScript 库中的函数(比如 Prototype)同样使用 $ 符号。 jQuery 使用名为 noConflict() 的方法来解决该问题。 var jq=jQuery.noConflict(),帮助您使用自己的名称(比如 jq)来代替 $ 符号。 亲自试一...