Having the following array of objects:
const variables = [
{ name: '%NAME%', value: 'joe' },
{ name: '%EMAIL%', value: '%NAME%@mail.com' },
{ name: '%HOBBY%', value: 'tennis' }
];
And the input string:
const inputString = `Hi, my name is %NAME%, I like %HOBBY%, you can contact me at %EMAIL%`;
The function should take as arguments variables and inputString and return the following sting:
'Hi, my name is joe, I like tennis, you can contact me at [email protected]'
Here is the function so far:
function doMagic(variables, inputString) {
let output = inputString;
for (let i = 0; i < variables.length; i++) {
output = inputString.replace(variables[i].name, variables[i].value);
}
return output;
}
Unfortunatelly, this only finds one occurrence, in case there are more, and it doesn't go into nested variables, like %EMAIL%.
Any ideas to improve?