5

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

How can I do it?

BrunoLM
  • 94,090
  • 80
  • 289
  • 441

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
  • 666,277
  • 106
  • 705
  • 986