1

Does someone have an idea how to remove leading zeros with regex but keen minus sign?

It so easy to do that for positive values

.replace(/^0+/,'')  

but I absolutely have no idea how to do that in negative case, for example

from -0.05 value to be -.05
from -02.05 value to be -2.05

I have created jsFiddle example for clarity

Denis
  • 2,429
  • 5
  • 33
  • 61

2 Answers2

15

Capture the negative sign and include it in the replacement:

n_string.replace(/^(-?)0+/,'$1');
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
3

The following could also do the job, in additional, it will allow you to have a 0 before .

n_string.replace(/^(-)?0+(?=\d)/,'$1');
loveNZ
  • 307
  • 2
  • 12