我需要验证输入,包含项目的价格。 价值可能是..
1.2
1.02
30,000
30,000.00
30000000
所以我需要正则表达式来支持这一点。
答案 0 :(得分:2)
那应该有用
/^[0-9]{1,3}(?:\,[0-9]{3})*(?:\.[0-9]{1,2})?$/
答案 1 :(得分:1)
想出了这个:
^\d+([\,]\d+)*([\.]\d+)?$
正则表达式用于检测它是否是一个价格。将其分解为部分:
^ # start of string
\d+ # this matches at least 1 digit (and is greedy; it matches as many as possible)
( # start of capturing group
[\,] # matcher group with an escaped comma inside
\d+ # same thing as above; matches at least 1 digit and as many as possible
)* # end of capturing group, which is repeated 0 or more times
# this allows prices with and without commas.
( # start of capturing group
[\.] # matcher group with an escaped fullstop inside
\d+ # same thing; refer to above
)? # end of capturing group, which is optional.
# this allows a decimal to be optional
$ # end of string
我建议您在创建正则表达式时尝试http://regex101.com。
答案 2 :(得分:0)
这应该有效
^(?:[1-9]\d*|0)?(?:\.\d+)?$