JavaScript string的replace和replaceAll的基本使用方法


一、replace

replace方法可以根据传入的第一个参数对原字符串进行匹配,然后使用第二个参数对匹配到的字符进行替换,返回一个新字符串。

语法:str.replace( str | reg ,str | func )
第一个参数可以是字符串或正则,第二个参数可以是字符串或函数

返回值:一个部分替换或者全部替换的新字符串。根据参数的类型不同,返回的结果也会有变化

//第一个参数是字符串,第二个参数也是字符串。
//这个时候仅仅会用第二个参数字符串把第一个匹配到的字符串给替换。
const str = 'to be or not to be.'
str.replace('be', 'BE')  // to BE or not to be.
//第一个参数是字符串,第二个参数是函数
const str = 'to be or not to be.'
const result = str.replace('be', function(match, index, str) {
	// 函数只会被调用一次
	// match:'o'  匹配的第一个字符串
	// index:1 匹配的字符串在原字符串开始位置到的索引
	// str:'to be or not to be.' 原字符串
	return match.toUpperCase()
})
result // to BE or not to be.
//第一个参数是正则, 第二个参数是字符串。这时候字符串可以写一些特殊的变量名
const str = 'to be or not to be.'
// 1:不带特殊变量名的字符串
str.replace(/be/, '**')  // 'to ** or not to be.' 正则不带g标志,只替换第一个匹配上的字符串
str.replace(/be/g, '**')  // 'to ** or not to **.' 带g标志,替换所有匹配的字符串
// 2:带特殊变量名的字符串,特殊变量名包括如下:
//$$:插入一个'$'
//$&:插入匹配的字符串
//$` 插入当前匹配的字符串前面的字符
//$' 插入当前匹配的字符串后面的字符
//$n 插入第n个括号匹配的字符串。如果不存在第n个分组,那么将会把匹配的字符串替换为$n的字面量内容。
//比如不存在第三个分组,那么将会把'$3'替换为匹配的字符串
//$<name> 这里name代表分组的名称。如果正则表达式中不存在分组或者没有匹配,这个变量将被处理为空字符串
str.replace(/be/g, '$$')  // 'to $ or not to $.'
str.replace(/be/g, '($&)')  // 'to (be) or not to (be).'
str.replace(/be/g, '($`)')  // 'to (to ) or not to (to be or not to ).'
str.replace(/be/g, "($')")  // 'to ( or not to be.) or not to (.).'
str.replace(/(to)\s(be)/g, '$1')  // 'to or not to.'
str.replace(/(to)\s(be)/g, '$3')  // '$3 or not $3.'
str.replace(/(?<T>to)\s(?<B>be)/g, '$<T>$<B>')  // 'tobe or not tobe.'  ?<T>是对分组命名的写法,T就是这个分组的名称
//第一个参数是正则,第二个参数是函数
const str = 'to be or not to be.'
const result = str.replace(/(to)\s(be)/g, function(match, g1, g2, index, str) {
	// 这里正则如果不带g(全局)标识符,就会只调用一次函数
	console.log('match:'+match+', g1:'+g1+', g2:'+g2+', index:'+index+', string:'+string);
	//match:to be, g1:to, g2:be, index:0, string:to be or not to be.
	//match:to be, g1:to, g2:be, index:13, string:to be or not to be.
	//match:匹配到的字符串,index:匹配到的字符串在原字符串中的开始位置的索引,string:原字符串
	//g1和g2表示的是正则里面的分组个数,有几个分组这里就要写几个参数。如果少了,后面的参数就会补上
	return g1 + '-' + g2
})
console.log(result)  // 'to-be or not to-be.'

二、replaceAll

replaceAll方法可以根据传入的第一个参数对原字符串进行匹配,然后使用第二个参数对匹配到的字符进行替换,返回一个新字符串。

语法:str.replaceAll( str|reg , str|func )
第一个参数可以是字符串或正则,第二个参数可以是字符串或函数

返回值:一个被全部替换的新字符串,不会改变原字符串。

// 第一个参数是字符串,第二个参数也是字符串。
//和replace不一样的是,replaceAll会替换掉所有匹配到的字符串
const str = 'to be or not to be.'
str.replaceAll('be', '**')  // 'to ** or not to **.'
//第一个参数是字符串,第二个参数是函数。
//和replace不一样的是,replaceAll每匹配一次就会调用一次函数。
const str = 'to be or not to be.'
const result = str.replaceAll('be', function(match, index, string) {
	return match.toUpperCase()
})
console.log(result)  // 'to BE or not to BE.'
//第一个参数是正则,第二个参数无论是字符串还是函数。
//这个时候正则必须要带上g(全局)标识符否则会报错,其余使用方式和replace方法一样。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript中,有两个字符串方法可以用于替换字符串中的子串:`replace()`和`replaceAll()`。 1. `replace()`方法: `replace()`方法字符串对象的方法,用于将指定的子串或正则表达式匹配的部分替换为新的字符串。它只会替换第一个匹配到的子串。 `replace()`方法的语法如下: ```javascript string.replace(searchValue, replaceValue) ``` 参数说明: - `searchValue`:要被替换的子串或正则表达式。 - `replaceValue`:用于替换的新字符串。 下面是一个示例用法: ```javascript const str = 'Hello, world!'; const newStr = str.replace('world', 'everyone'); console.log(newStr); // 输出: 'Hello, everyone!' ``` 在上面的示例中,`replace()`方法字符串中的'world'替换为'everyone'。 2. `replaceAll()`方法: `replaceAll()`方法字符串对象的方法,用于将所有匹配到的子串或正则表达式匹配的部分替换为新的字符串。它会替换所有匹配到的子串。 `replaceAll()`方法的语法如下: ```javascript string.replaceAll(searchValue, replaceValue) ``` 参数说明: - `searchValue`:要被替换的子串或正则表达式。 - `replaceValue`:用于替换的新字符串。 下面是一个示例用法: ```javascript const str = 'Hello, world!'; const newStr = str.replaceAll('o', 'e'); console.log(newStr); // 输出: 'Helle, werld!' ``` 在上面的示例中,`replaceAll()`方法字符串中的所有'o'替换为'e'。 需要注意的是,`replace()`和`replaceAll()`方法都会返回一个新的字符串,并且原始字符串不会被修改。另外,`replace()`方法可以接受正则表达式作为`searchValue`参数,而`replaceAll()`方法在一些旧版本的JavaScript中可能不被支持,可以通过使用正则表达式和全局匹配来实现类似的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值