我需要验证输入,其中包含项目的价格。该值可以是..
1.2
1.02
30,000
30,000.00
30000000
所以我需要正则表达式来支持这一点。
想到了这个:
^\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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句