Skip to content
Advertisement

Regex Extraction – Mixed Decimal seperator

I am currently trying to write a regex in js that would extract the decimal numbers from a mixed string.

Example strings are following

JavaScript

My desired output is following

JavaScript

If I run the following regex

JavaScript

it doesnot return any match when the decimal point is followed by a single digit.Eg. following cases

JavaScript

I am not sure how to tune the regex that works for all such cases. If someone can please help me would be very helpful. The full js here

JavaScript

Regex101screenshot

Also I am getting this in regex101 which I am not sure how to decipher

JavaScript

Advertisement

Answer

You can use

JavaScript

See the regex demo.

Details:

  • d+ – one or more digits
  • (?:,d+)* – zero or more occurrences of a comma and one or more digits
  • (?:.d+)? – an optional occurrence of a dot and one or more digits.

See the JavaScript demo:

JavaScript
Advertisement