UK Currency Regular Expression for javascript

I get a regular expression that matches the British currency (e.g. £ 13.00, £ 9999.99 and £ 12.333.333.02) but does not allow negative (- 2.17) or zero values ​​(0.00 or 0).

I tried to create it myself, but I have the right confusion!

Any help greatly appreciated.

Thanks!

+3
source share
3 answers

This will do it (well basically ...)

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

Uses ^ and $ to ensure that there is no negative or other character in the string, and assumes that commas will be used. The pound and pence symbols are optional.

edit: it is implemented that you said a non-zero value, therefore replaced the first \ d with [1-9]

: , 0,01. , , , , /[1-9]/first, - .

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

:

  • "£"
  • :
    • a > £ 1 pence
    • a < £ 1 >= £ 0.10
    • a <= £ 0,09

( ) , .

+5

Unix/Linux "£" JavaScript, , :

/^\XA3\ {1,3} ([,]\d {3} |\)??? * ([.]\ {1,2}) $/

, , .

+4

You could just do two passes:

/^£\d{1,3}(,\d{3})*(\.\d{2})?$/

to confirm the format and

/[1-9]/

so that at least one digit is nonzero.

This is less efficient than doing it in one go, of course (thanks, annakata, for the test information), but for the first implementation, just “say what you want” can significantly reduce development time.

0
source

Source: https://habr.com/ru/post/1710462/


All Articles