I have a text that has format characters in curly braces.
let str = "My name is {name} and age is {age}!";
I want to change this with an object properties.
var myobj = { name: "x", age: "25" }
The result text should be "My name is x and age is 25!"
I can get the name and age values in text using regex.
let regex = /\{([^}]+)\}/g;
let matches = str.match(regex).map(x => x.replace(/[{}]/g, ""));
matches is array ["name", "age"]
But how can I replace with object properties using regex? Is possible a short way?
`My name is {myobj.name} and age is {myobj.age}!`or even`My name is {name} and age is {age}!`if you make surenameandageare variables.str.replace(regex, (x,y) => myobj[y] ? myobj[y] : x)