small medium large xlarge

Generic-user-small
04 Jul 2012, 11:36
Jeff Lim (9 posts)

I believe that instead of /$[,\d]+.\d\d/, /$\d+.\d\d/ is sufficient.

Generic-user-small
07 Jul 2012, 01:42
John Goetz (6 posts)

The [,\d]+ is necessary to allow for multiple digits (and grouping) before the decimal point. Your suggestion matches prices with exactly one digit before the decimal (and no grouping markers), so $9.99 and $4.32 match but $15.99 and $1,099.45 would not.

Note that $[,\d]+.\d\d isn’t totally correct, either: it matches incorrect prices like $5,4,,3.79 or $,.45. A more precise regex is $\d{1,3}(,\d{3})*.\d\d, but this still allows $00,123.45. The correct regex is left as an exercise for the reader… 8^)

Generic-user-small
07 Jul 2012, 06:47
Jeff Lim (9 posts)

multiple digits PLUS grouping, you mean! But thanks for the clarification! I forgot about the ‘number_to_currency’ helper, and the ‘,’s, that it introduces…

And yeah, apparently I mistyped my alternative regex (my bad!). Definitely should have a plus behind the first ‘\d’ (of course?)!

You must be logged in to comment