replaceAll is not a function 详解

先说说原因:

在chrome 浏览器中使用 replaceAll 报这个错误,是因为chrome 版本过低, 在chrome 85 以上版本才支持

用法

replaceAll(pattern, replacement)

const paragraph = "I think Ruth's dog is cuter than your dog!";
console.log(paragraph.replaceAll('dog', 'monkey'));

"aabbcc".replaceAll("b", ".");  // 'aa..cc'

浏览器支持情况:
在这里插入图片描述
那在不直支持该特性的浏览器中有哪些解决方式呢?
直接替代法:
使用regexp全部无差别替换

let unformattedDate = "06=07=2022";
const formattedString = unformattedDate.replace(/=/g, ':');
console.log(formattedString);   //06:07:2022

手动定义法:
你可以对其进行自定义:

if(typeof String.prototype.replaceAll === "undefined") {
    String.prototype.replaceAll = function(match, replace) {
       return this.replace(new RegExp(match, 'g'), () => replace);
    }
}
String.prototype.replaceAllTxt = function replaceAll(search, replace) { return this.split(search).join(replace); }

本质上都是一样的,都是用的匹配替换的方式来代替

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]:replaceAll是一个字符串的方法,用于替换字符串中的所有匹配项。然而,它在某些浏览器版本中可能不被支持。引用\[2\]中提到,Chrome浏览器仅在版本85以上支持replaceAll方法。所以,如果你在低于85版本的Chrome浏览器中使用replaceAll方法,就会报错"replaceAll is not a function"。为了解决这个问题,你可以使用其他方法来替换字符串中的所有匹配项。例如,你可以使用split和join方法,或者使用正则表达式的replace方法。引用\[2\]中给出了两个解决方法。第一个方法是使用split和join方法,将字符串分割成数组,然后用指定的替换字符连接数组元素。第二个方法是使用正则表达式的replace方法,通过指定全局匹配标志"g"来替换所有匹配项。另外,你也可以考虑升级你的浏览器版本以支持replaceAll方法。\[2\] #### 引用[.reference_title] - *1* [解决replaceAll报错:string.replaceAll is not a function](https://blog.csdn.net/qq_34626094/article/details/128672647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [巧遇replaceAll报错:string.replaceAll is not a function](https://blog.csdn.net/weixin_49985592/article/details/127100456)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [replaceAll is not a function 问题解决](https://blog.csdn.net/weixin_44921956/article/details/126245067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kirinlau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值