0

I am looking for a regex to mark all zeros from the right until it faces a non zero.

For example,

5819323.0100-

the regex should detect that it exists two zero from the right and it should start to validate after the ..

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • But there *aren't* two zeroes from the right. The right most character is `-` - should it be ignored? – VLAZ Nov 10 '20 at 16:38
  • It looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Nov 10 '20 at 16:50

1 Answers1

0

I don't believe you meant to keep that dash at the end…

Use a positive lookbehind of (?<=\.\d*) and end the expression with an end of line i.e. $. The look behind checks for a sequence of zeros at the end of the string that are preceded by a decimal point and zero or more digits.

const
  input = '5819323.0100',
  output = input.replace(/(?<=\.\d*)0+$/, '');

console.log(output); // 5819323.01

If you are using this as a number, just parse it; the zeroes will be ignored.

const
  input = '5819323.0100',
  output = parseFloat(input);

console.log(output); // 5819323.01
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132