Replace multiple string and regexp

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
paolo2504
Posts: 18
Joined: 27 Jan 2022 11:23

Replace multiple string and regexp

#1 Post by paolo2504 » 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];
});

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

Re: Replace multiple string and regexp

#2 Post by siberia-man » 11 Mar 2022 10:28

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.

paolo2504
Posts: 18
Joined: 27 Jan 2022 11:23

Re: Replace multiple string and regexp

#3 Post by paolo2504 » 11 Mar 2022 12:35

I got this suspect…. Many thanks : I’ll study nodejs.

Post Reply