I have the following snippet from this post, this code replaces the token $ENV[accountsDomain]
in a string with the key from item
object accountsDomain: 'www.yahoo.com'
and similarly for accountsUrl
how can I run replace conditionally, replace the string only if item[g1]
exists, as these keys are optional
const item = {
accountsUrl: 'www.google.com',
accountsDomain: 'www.yahoo.com'
}
const regex = /\$ENV\[(\w+)]/
let s = "foo$ENV[accountsDomain]bar";
s = s.replace(regex, (m, g1) => item[g1] || m);
console.log(s);