How to Solve JavaScript Error: replaceAll is not a function

Why do the above error reports occur

If you see the error “typeerror: replaceall is not a function”, it may be that your browser version or node.js version does not support this method.

we should note that: string. Prototype. Replaceall()   The method is added in ES2021/ES12, which is probably not supported by the environment.

How to solve it?

You can use “string. Prototype. Replace()” as a substitute for “string. Prototype. Replaceall()”, replace()   Just set a global (g) in the replaced regular expression. This processing method is the same as that of replaceall(). Here is an example of comparison:

const str = 'foo-bar';

// in older browsers
const result1 = str.replace(/foo/g, 'moo');

// ES12+
const result2 = str.replaceAll('foo', 'moo');

// output: 'moo-bar'

Similar Posts: