Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace {0} but not {{0}}?

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

How can I do it?

like image 892
BrunoLM Avatar asked Nov 25 '10 18:11

BrunoLM


People also ask

How do I replace only 0 in Excel?

Choose Find/Replace (CTRL-H). Use 0 for Find what and leave the Replace with field blank (see below). Check “Match entire cell contents” or Excel will replace every zero, even the ones within values.

How do I return blank instead of zero?

Under Display options for this worksheet, select a worksheet, and then do one of the following: To display zero (0) values in cells, check the Show a zero in cells that have zero value check box. To display zero (0) values as blank cells, uncheck the Show a zero in cells that have zero value check box.

How do I replace #value with 0 in Excel?

Step 1: Select the range that you will work with. Step 2: Press the F5 key to open the Go To dialog box. Step 3: Click the Special button, and it opens the Go to Special dialog box. Step 6: Now just enter 0 or any other value that you need to replace the errors, and press Ctrl + Enter keys.


1 Answers

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});
like image 96
Guffa Avatar answered Sep 26 '22 14:09

Guffa