- 浏览: 32181 次
文章分类
最新评论
【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);
}
相关推荐
标题中提到的“字符串replaceAll函数代码分享”实际上指的是在JavaScript中原生不支持replaceAll函数,但是通过自定义的String扩展方法可以实现这一功能。下面是自定义replaceAll函数的方法,它通过扩展String的原型...
标题所指向的知识点是关于JavaScript语言中如何实现字符串替换操作的replaceAll方法。描述部分提到,文章将对这个方法进行详细总结,并提供实例代码以供参考,以便帮助需要此功能的朋友。标签为“JS replaceAll方法...
JavaScript中的字符串是编程中常见且重要的数据类型,它包含了丰富的属性和方法,使得开发者能够方便地操作和处理文本。以下是一些主要的字符串属性和方法的详细解释: 1. **属性:** - `String.length`:这个属性...
因此,如果需要实现replaceAll效果,即替换字符串中所有匹配的子串,可以使用正则表达式配合replace()方法来实现。 正则表达式中的全局匹配标志(g)用于指示匹配操作应该在输入字符串中进行全局搜索,即查找到所有...
6. **处理字符串**:在处理方法中,获取两个文本字段的值,使用`String`类的`replace()` 或 `replaceAll()` 方法删除指定字符,然后更新结果。 7. **显示结果**:在Applet的某个地方(如`JLabel`)显示处理后的字符...
无论是开发简单的文本编辑器还是复杂的文本分析应用,理解并掌握查找和替换字符串的方法至关重要。 一、基本概念 1. 字符串:在编程中,字符串是由一个或多个字符组成的序列,通常用来表示文本数据。 2. 查找:在...
### JavaScript常用字符串与数组扩展函数小结 #### 引言 在现代Web开发中,JavaScript是一种必不可少的语言。作为一门功能强大的脚本语言,它提供了大量的内置对象和方法来处理各种数据类型,其中最常用的就是字符...
Strman是一个专为JavaScript设计...总的来说,Strman是一个强大且全面的JavaScript字符串处理工具,它提供了一系列实用的函数,帮助开发者更高效地处理字符串任务,无论是前端开发还是Node.js后端开发,都能从中受益。
此外,Java 中还提供了许多其他的字符-字符串处理方法,例如 String 类的 `replaceAll()` 方法、`replaceFirst()` 方法、`split()` 方法等,这些方法可以实现各种字符串处理操作。 Java 中的字符-字符串常用方法为...
我们可以将字符串转换为字符数组,然后通过`stream()`方法创建一个流,再通过`filter()`方法过滤掉指定的字符,最后用`collect()`方法收集结果。 ```java public String removeCharWithStream(String str, char c) ...
这是因为在JavaScript字符串中,反斜线是转义字符,所以需要用两个反斜线来表示一个实际的反斜线。 除了直接在代码中使用replace方法以外,有时候,开发者可能会在浏览器的地址栏中直接执行JavaScript代码,以便...
总结一下,`replace()`方法和`replaceAll()`函数在JavaScript中用于字符串替换,其中`replace()`仅替换第一个匹配项或根据正则表达式全局替换,而`replaceAll()`则始终进行全局替换,简化了全局替换的操作。...
本文介绍了如何在JavaScript中实现一个格式化字符串的函数String.format,其主要功能是通过占位符的方式动态插入变量,同时解决了引号转义的问题,提高了代码的整洁性和可读性。 首先,问题的提出是在需要通过...
通常情况下,我们会使用字符串的`replace()`方法来实现替换功能。然而,`replace()`方法默认只替换第一个匹配项。如果需要替换所有的匹配项,我们必须使用正则表达式,并且加上`g`标志。 首先,来看一下`replace()`...
在JavaScript中,字符串操作是常见的任务,其中replace()函数是实现字符串替换功能的重要工具。本文详细介绍和实例演示了如何使用JavaScript中的replace()函数来替换字符串。 首先,replace()函数是String对象的一...
- 使用`String.format()`方法配合`%n`或其他格式化符号,可以实现字符串的填充。例如,`String.format("%1$-10s", "hello")`会在左侧填充空格,使"hello"长度至少为10。 - `StringBuilder`或`StringBuffer`类的`...
在JavaScript中,经常需要将JSON字符串转换为JSON对象,以便能够操作和处理这些数据。然而,在这个过程中,可能会遇到一些转义问题,特别...通过以上方法,可以有效地解决JavaScript中JSON字符串到JSON对象的转义问题。
替换 JavaScript 字符串中的所有实例。 使用 npm 安装 npm install replaceall 然后在您的节点应用程序中包含 replaceall: var replaceall = require("replaceall"); 使用替换 var result = replaceall ( ...