javascript中使用replaceAll()函数实现字符替换的方法
作者:mdxy-dxy 发布时间:2024-04-10 16:18:47
而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>
补充
我们在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”
只需要执行一次,就完成了全部替换需求。


猜你喜欢
- 一、网络知识的一些介绍 socket 是网络连接端点。例如当你的Web浏览器请求www.jb51.net上的主页时,你的Web浏览器创建一个
- 本文实例为大家分享了python实现移位加密和解密的具体代码,供大家参考,具体内容如下代码很简单,就不多做解释啦。主要思路是将字符串转为As
- 一、SQLAlchemy 介绍1.1 ORM 的概念ORM全称Object Relational Mapping(对象关系映射),通过 OR
- 闭包闭包就是能够读取其他函数内部变量的函数。def test1(k, b): def test1_1(x): &n
- 二进制核心思想:冯诺依曼 + 图灵机电如何表示状态,才能稳定?计算机开始设计的时候并不是考虑简单,而是考虑能自动完成任务与结果的可靠性,简单
- 假设要实现一个存放多种类型数据结构的对象,比如一个存放算术操作数和操作符的树结点,需要存放包含一元操作符、二元操作符和数字类型的结点clas
- golang时间格式化科普 CST 含义CST: 中部标准时间 (Central Standard Time) 同时表示下面4个时区CST
- 简单计数器代码如下所示:<% Set fs = CreateObject("Scri
- 方案有很多种,我这里简单说一下:1. into outfileSELECT * FROM mytable  
- 一、数据可视化1.pyecharts介绍官方网址:https://pyecharts.org/#/zh-cn/intro📣 概况:Echar
- 前言本文旨在用最通俗的语言讲述最枯燥的基本知识这个话题比较有意思。昨天中午吃完饭间突然有个同事蹦出了一句:“like有索引吗?”,我顺口就说
- 省市区县数据来源Google地图. (包括34个省 , 371个市, 2824个县区) /**********创建省级表**********
- myisam_max_[extra]_sort_file_size足够大 delay_key_write减少io,提高写入性能 bulk_i
- 在这篇文章(不敢妄称教程,最多称之为学习笔记)里,我会从头开始实现客户端模板的效果。不过你不要期望能够在这里找到可以直接拿去使用直接复用灵活
- 前言本文将使用pytorch框架的目标识别技术实现滑块验证码的破解。我们这里选择了yolov5算法例:输入图像输出图像可以看到经过检测之后,
- 前言当我们想要创建一个对象,我们可能使用new方法去构建一个对象,那按道理jquery也是一个对象,应该也是用new jquery()来构建
- 在学习傅里叶变换的时候遇到了求周期方波信号频谱图的例子,在书上和网上查阅了一些资料,发现大都是讨论的都是下图左边的周期信号的频谱,课程老师的
- 前言一向用Python 3,最近研究微信公众号开发,各云平台只支持Python 2.7,想用其他版本需要自己搭建环境。而网上又搜不到Pyth
- 利用Python将Market1501的分割图片和原图两张图片进行拼接成一左一右一张图片,并将图片的像素值调整成256*128.所有文件夹:
- 处理数据的时候,偶然遇到要把一个Dataframe中的某些行添加至一个空白的Dataframe中的问题。最先想到的方法是创建Datafram