Js string replaces all

The replace of

js can only be replaced by the first one. How do you replace all of them? the way of RegExp is similar to "(" is there a mistake? is there a more elegant way to write it?

String.prototype.replaceAll = function (FindText, RepText) { 
    regExp = new RegExp(FindText, "gm"); 
    return this.replace(regExp, RepText); 
}
// 
var s = "abcdefabcedf(123)";
console.log(s.replaceAll("b", "B"));    // 
console.log(s.replaceAll("(", "["));    // 
/*
Uncaught SyntaxError: Invalid regular expression: /(/: Unterminated group
    at new RegExp (<anonymous>)
    at String.replaceAll (<anonymous>:2:11)
    at <anonymous>:7:15
*/

I always feel that some of the basic language functions of js are painful to use.

Mar.04,2021

replaces all, that is, the regular expression is marked with global g . For "and' are \" and\'


.

that's fine, but I don't know what the efficiency is:

String.prototype.replaceAll = function (FindText, RepText) { 
    return this.split(FindText).join(RepText); 
}
Menu