0

I'm trying to replace every word between %% with a single word. Till now I tried this:

var string = "You chose %story% at %price%";

var rep = string.replace(/\%(.*)\%/g, "test");

console.log(rep);

but the result of rep is actually

"You chose test"

while I want

"You chose test at test"

How can I achieve this? I though about implementing a recursive function but it sound pretty slow especially with multiple words

steo
  • 4,586
  • 2
  • 33
  • 64

1 Answers1

0

Try the snippet below, just put a lazy quantifier ? after *, so that it will not take more than one occurrence

var string = "You chose %story% at %price%";

var rep = string.replace(/%(.*?)%/g, "test");

console.log(rep);
ild flue
  • 1,323
  • 8
  • 9