I have an input like this:
var input = "text {{ variable*100 }} text {{ variable }} text {{ variable+2 }}";
and I have a variable :
var varible = 2;
I want to replace the text inside the {{}}
with the value of the variable and calculate if needed
I'm expecting the result to be :
"text 200 text 2 text 102"
I've tried this code :
var tooltip = 'text {{ damage*100 }} text {{ damage }} text {{mana-1}}';
var objMap = {
damage: 100,
defence: 26,
mana: 100,
}
var replaced = tooltip.replace(/(\{{.*?\}})/g, function (m, $1) {
var key = $1.slice(2, -2).trim();
return objMap[key];
});
console.log(replaced)
and I got this result :
text undefined text 100 text undefined