📜  如何修复“replaceAll 不是函数”JavaScript 错误? - Javascript代码示例

📅  最后修改于: 2022-03-11 15:04:10.159000             🧑  作者: Mango

代码示例1
// If you're seeing the error "TypeError: replaceAll is not a function", it is likely due to the method not implemented/supported by the browser version (or the Node.js version) that you're using.
// Note that the String.prototype.replaceAll() method was added in ES2021/ES12.
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);