zoukankan      html  css  js  c++  java
  • 怎么修复 "replaceAll is not a function" JavaScript Error?

    为什么会出现上面的报错情况

    如果你看到了“TypeError: replaceAll is not a function”这个错误,可能是你的浏览器版本或者Node.js版没有支持此方法。

    我们要注意的是:String.prototype.replaceAll() 方法是在ES2021/ES12中被加入的,很有可能的环境不支持。

    怎么解决呢?

    你可以使用“String.prototype.replace()”作为“String.prototype.replaceAll()”的替代品,replace() 只需要在替换的正则表达式中设置一个全局(g)就可以了。这种处理方式了replaceAll()的处理结果相同。下面来看个对比的例子:

    const str = 'foo-bar';
    
    // in older browsers
    const result1 = str.replace(/foo/g, 'moo');
    
    // ES12+
    const result2 = str.replaceAll('foo', 'moo');
    
    // output: 'moo-bar'
    console.log(result1);
    console.log(result2);
  • 相关阅读:
    3D文字菜单变换
    妙味课堂作业20160113(优化版)
    pop up layer loading
    妙味课堂作业20160113
    妙味课堂20160112js实例仿新浪菜单
    js面向对象初探
    sea.js demo
    注册ASP.NET
    JDK1.6 环境配置
    JDK1.7环境配置
  • 原文地址:https://www.cnblogs.com/duhuo/p/15452031.html
Copyright © 2011-2022 走看看