I would have make the "escapeForRegExp" function built-in (it seems to me the kind of thing should be built-in), perhaps called RegExp.quote. (I would also to add support for many features of PCRE.)
How do all the other languages that have an equivalent function (Perl quotemeta, PHP preg_quote, Python re.escape, Ruby: Regexp.escape, Java Pattern.quote, C# Regex.Escape, Go QuoteMeta, Rust regex::escape) avoid or deal with this problem?
The first couple of comments as well as the very last one from April in that thread all sum it up quite well.
new RegExp(String.raw`\s`)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... function replaceAll(str: string, pattern: string, replacement: string): string {
return str.replace(new RegExp(escapeForRegExp(pattern), "g"), replacement);
}