5

I want to replace all {0} on a string with x. It should not match {{0}}.

How can I do it?

BrunoLM
  • 97,872
  • 84
  • 296
  • 452

1 Answers1

13

Match either {{0}} or {0}, and replace only those occurances that are {0}.

Something like:

s = s.replace(/(\{\{0\}\}|\{0\})/g, function(m){ return m == '{0}' ? 'x' : m});
Guffa
  • 687,336
  • 108
  • 737
  • 1,005