I am looking the consequences of using new RegExp()
vs RegExp()
in this scenario:
function removeWord(str,rexp){
return str.replace(rexp,"")
}
var example1="I like tasty peanuts!";
var banned_word="peanuts";
var build_rexp_from_var=new RegExp(banned_word,"g");
//method #1 -> removeWord(example1,/peanuts/g)
//method #2 -> removeWord(example1,build_rexp_from_var)
//which should be my method #3?
console.log(removeWord(example1,RegExp(banned_word,"g")));
console.log(removeWord(example1,new RegExp(banned_word,"g")));
I want to avoid creating the var build_rexp_from_var
because it is unnecessary. Both seems to work, but I want to know the differences that might have using one over the other.