I'm trying to replace all these strings:
'Apples are {{appleColor}}'
'Oranges are {{orangeColor}}'
'My name is {{name}}'
With these:
obj = {
appleColor: 'red',
orangeColor: 'orange',
name: 'Todd'
}
Thanks in advance!
Simple solution using String.prototype.replace() function with replacement callback:
var str = "'Apples are {{appleColor}}', 'Oranges are {{orangeColor}}', 'My name is {{name}}' ",
obj = {
appleColor: 'red',
orangeColor: 'orange',
name: 'Todd'
};
str = str.replace(/{{(\w+)}}/g, function (m, m1) {
return obj[m1] || m;
});
console.log(str);
_ or $ support), no object path depth, etc.
${}