There are any numbers, how to start with finding all zeros using a regular expression?
For example, such a number is 0.001 .

Regular expression:. .[0].* , But one is selected.

  • What should be the result? 0.1 ? - Wiktor Stribiżew

3 answers 3

Well, if I understand the question correctly ...

 var x = 0.001 var s = x + '' console.log(s.replace(/\.0+/, '.')) 

  • Or this: 0 * \.? 0 *;) - JavaJunior
  • Well or so: (?<=(?<=\d)\.)[0]{1,} - LLENN 2:22 pm
  • @LLENN, some kind of brrrrrrrrrr ... - Qwertiy
  • @Qwertiy: Well, just in your expression, the point also falls under the expression. - LLENN 2:24 pm
  • @LLENN 1. so what? 2. Support for previewing back to js is highly questionable. 3. I don’t understand why you need two nested previews. - Qwertiy

Option to delete zeros after a point before the first meeting of the digit (except zero)

 var string = '0.001'; console.log(string.replace(/(?<=\.)[0]+/, '')); 

Option to remove absolutely all zeros after the decimal point.

 var string = '0.001337014880'; console.log(string.replace(/(?!^0)(?<=\.?)[0]+/g, '')); 

  • Why shove a preview back to where it is not needed? Moreover, it is still supported in few places. - Qwertiy 1:02 pm

Try the following:

 (?<=\.)[0]* 

https://regex101.com/r/xxck6h/3

  • 1. Wrong: 100.001 . 2. Why shove a preview back to where it is not needed? Moreover, it is still supported in few places. - Qwertiy