JS中实现replaceAll的方法(实例代码)

 更新时间:2023年06月14日 01:04:12   投稿:jingxian  
本文是对JS中实现replaceAll的方法进行了详细的总结介绍,需要的朋友可以过来参考下,希望对大家有所帮助

我们在Java中可以使用replaceAll()方法对字符串进行批量替换,但在JS中replaceAll()方法是undefined,JS中只存在replace()方法,因此我们可以自己封装JS中replaceAll()方法供我们便捷使用。

一、使用replace()方法进行替换

定义一个字符串:

var str = "hello world";

使用replace()方法将字符串中的字母"l"替换成"i",原始做法:

 console.log(str.replace("l","i"));

输出:

“heilo world”

需要执行三次,非常不方便;

二、使用replaceAll()方法替换

封装replaceAll()方法:

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

定义一个字符串:

var str = "hello world";

使用replaceAll()方法进行批量替换:

console.log(str.replaceAll("l", "i"));

输出:

“heiio worid”

只需要执行一次,就完成了全部替换需求。

补充

第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符.
而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。

replace()

The replace() method returns the string that results when you replace text matching its first argument
(a regular expression) with the text of the second argument (a string).
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first
occurrence of the pattern. For example,

var s = "Hello. Regexps are fun.";s = s.replace(/\./, "!"); // replace first period with an exclamation pointalert(s);

produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to
perform a global replace, finding and replacing every matching substring. For example,

var s = "Hello. Regexps are fun.";s = s.replace(/\./g, "!"); // replace all periods with exclamation pointsalert(s);

yields this result: “Hello! Regexps are fun!”

所以可以用以下几种方式.:

string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);

string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。

<script type="text/javascript"> 
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { 
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) { 
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); 
    } else { 
        return this.replace(reallyDo, replaceWith); 
    } 
} 
</script> 

到此这篇关于JS中实现replaceAll的方法(实例代码)的文章就介绍到这了,更多相关JS replaceAll方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 原生js调用json方法总结

    原生js调用json方法总结

    本篇文章给大家详细分析了js调用json方法的总结,对此有需要的朋友可以参考学习下。
    2018-02-02
  • JS的空值合并运算符(??)的使用

    JS的空值合并运算符(??)的使用

    空值合并运算符是一个逻辑运算符,当左侧的操作数为null或undefined时,会返回右侧操作数,否则返回左侧操作数,本文就来详细的介绍一下如何使用
    2023-12-12
  • js+canvas绘制五角星的方法

    js+canvas绘制五角星的方法

    这篇文章主要介绍了js+canvas绘制五角星的方法,涉及JavaScript调用canvas组件结合数学运算绘制图形的相关技巧,需要的朋友可以参考下
    2016-01-01
  • 基于MooTools的很有创意的滚动条时钟动画

    基于MooTools的很有创意的滚动条时钟动画

    一款很有创意的时钟js动画.是利用系统滚动条来构成一个 时:分:秒 的盘. 再利用滚动条的长度变化做过渡动画.
    2010-11-11
  • javascript数组遍历的方法实例分析

    javascript数组遍历的方法实例分析

    这篇文章主要介绍了javascript数组遍历的方法,结合实例形式分析了javascript数组遍历及相关的some、every、filter、map等方法的使用技巧,需要的朋友可以参考下
    2016-09-09
  • Javascript动手实现call,bind,apply的代码详解

    Javascript动手实现call,bind,apply的代码详解

    这篇文章主要为大家详细介绍了Javascript动手实现call,bind,apply的代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Cookie 小记

    Cookie 小记

    Cookie 经常用,也就是把关键信息记录进去,确认不在保留信息,则设置使之过期。
    2010-04-04
  • Webpack中loader打包各种文件的方法实例

    Webpack中loader打包各种文件的方法实例

    这篇文章主要给大家介绍了关于Webpack中loader打包各种文件的相关资料,其中包括处理css文件、less文件、scss文件、url地址以及ES6高级语法的方法,需要的朋友可以参考下
    2019-09-09
  • 使用BootStrap实现用户登录界面UI

    使用BootStrap实现用户登录界面UI

    本文给大家介绍使用BootStrap实现用户登录界面UI,布局风格采用左右各一半的风格设计,非常不错,具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-08-08
  • uniapp中canvas绘制图片内容空白报错的原因及解决

    uniapp中canvas绘制图片内容空白报错的原因及解决

    最近有个需求就是要用canvas画个分享的海报,所以这里总结下,这篇文章主要给大家介绍了关于uniapp中canvas绘制图片内容空白报错的原因及解决方法,需要的朋友可以参考下
    2023-09-09

最新评论