diff --git a/src/common/polyfills.ts b/src/common/polyfills.ts index 3a5bde3e7..8728cfe54 100644 --- a/src/common/polyfills.ts +++ b/src/common/polyfills.ts @@ -125,6 +125,11 @@ import "./polyfill-TextEncoderDecoderStream"; // Chrome 85, Safari 13.1 if (!String.prototype.replaceAll) { + const escapeRegex = (string: string) => { + // eslint-disable-next-line unicorn/prefer-string-replace-all + return string.replace(/[$()*+./?[\\\]^{|}-]/g, String.raw`\$&`); + }; + (String as any).prototype.replaceAll = function ( str: string | RegExp, newStr: string, @@ -138,7 +143,7 @@ if (!String.prototype.replaceAll) { // If a string // eslint-disable-next-line unicorn/prefer-string-replace-all - return this.replace(new RegExp(str, "g"), newStr); + return this.replace(new RegExp(escapeRegex(str as string), "g"), newStr); }; }