String.prototype.replaceAll() 兼容 chrome 什么版本?

1,530 阅读1分钟

String.prototype.replaceAll() 兼容 chrome 什么版本?

Chrome 85

Edge 85

Firefox 77

IE 不兼容

Opera 71

Safari 13.1

developer.mozilla.org/zh-CN/docs/…

polyfill

Pushes support back to at least IE6.

/**
 * String.prototype.replaceAll() polyfill
 * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
 * @author Chris Ferdinandi
 * @license MIT
 */
if (!String.prototype.replaceAll) {
  String.prototype.replaceAll = function(str, newStr){
    // If a regex pattern
    if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
      return this.replace(str, newStr);
    }
    // If a string
    return this.replace(new RegExp(str, 'g'), newStr);
  };
}

vanillajstoolkit.com/polyfills/s…