regex

Regex for cryptocurrencies


I want to validate some fraction of cryptocurrencies, for example, I have the following VALID values

0,000001
0,000000001
1,00
1.000,00

Right now I have this regex for validating currency (yes, with comma instead of dot)

/^[1-9]\d{0,2}(\.\d{3})*,\d{2}$/

How can I do the same for the values above?


Solution

  • You might either match the more exact pattern starting with a digit 1-9, or match a pattern starting with a zero and have 1 or more digits after the comma:

    ^(?:[1-9]\d{0,2}(?:\.\d{3})*,\d{2}|0\d*,\d+)$
    

    Regex demo