Page 1 of 1

Replace multiple string and regexp

Posted: 09 Mar 2022 11:28
by paolo2504
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];
});

Re: Replace multiple string and regexp

Posted: 11 Mar 2022 10:28
by siberia-man
Object.keys is not defined in ms/jscript. The code you posted throws error because ms/jscript doesn't support many features. However it can be successfully run under nodejs. You need use nodejs or define Object.keys on your own.

Re: Replace multiple string and regexp

Posted: 11 Mar 2022 12:35
by paolo2504
I got this suspect…. Many thanks : I’ll study nodejs.