Replace multiple string and regexp
Posted: 09 Mar 2022 11:28
On another jscript forum i found the following sample code that helps me greatly on a problem i have posted on this forum.
However, running this piece of code i am getting error:
microsoft jscript runtime error property or method not supported by object.
Error arises on line with RegExp. I don't understand why.
any help appreciated.
var mapObj = {cat:"dog",dog:"goat",goat:"cat"};
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
str = str.replace(re, function(matched){
return mapObj[matched];
});
However, running this piece of code i am getting error:
microsoft jscript runtime error property or method not supported by object.
Error arises on line with RegExp. I don't understand why.
any help appreciated.
var mapObj = {cat:"dog",dog:"goat",goat:"cat"};
var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
str = str.replace(re, function(matched){
return mapObj[matched];
});