I am using a piece of code to replace "Tags" by values in Strings My concern is that it only changes the first Tag encountered if it exists several times :(
const replaceTagsToValue = (obj, chaine) => obj.reduce((f, s) => `${f}`.replace(Object.keys(s)[0], s[Object.keys(s)[0]]), chaine)
const Tags = [
{ "{info_1}": "One" },
{ "{info_2}": "Two" },
{ "{info_3}": "Three" }
// etc etc...
]
let input = "{info_1}-{info_2}-{info_3} | {info_3}-{info_2}-{info_1}";
let output = replaceTagsToValue(Tags, input);
console.log(output);
Result:
One-Two-Three | {info_3}-{info_2}-{info_1}
Want these result:
One-Two-Three | Three-Two-One
I tested with the addition of a global regex approach, without success of course:
.replace(/Object.keys(s)[0]/g, s[Object.keys(s)[0]])
Do you have a solution ? see a more suitable and more generic or best practice solution?
Thank's !
replaceTagsToValue, shouldn't your expected output beOne-Two-Three | Three-Two-One?