I have an object that is evaluated from JSON that looks like this:
{
"foo %s": "bar %s",
"Hello %s world %s.": "We have multiple %s %s."
}
Using this object and the following inputs, I am trying to achieve something like this:
foo bar-> matchesfoo %s->bar barHello foo world bar.-> matchesHello %s world %s.->We have multiple foo bar
So far I have the following code which works for one %s and only if it is surrounded by spaces.
let str = "input here";
let a = {
"foo %s": "bar %s",
"Hello %s world %s.": "We have multiple %s %s."
},
p = str.split(" "),
result;
for (let j in p) { // loop indices
let pp = p.map(u => u); // create local array
pp[j] = "%s"; //replace index with %s
if (a[pp.join(" ")]) { // check if this is in the object
result = a[pp.join(" ")].replace("%s", p[j]); // join the array and replace %s from the result
break;
}
}
Any ideas or suggestions?