-1

我需要验证一个输入,包含一个项目的价格。价值可能是..

1.2
1.02 
30,000 
30,000.00 
30000000

所以我需要正则表达式来支持这一点。

4

3 回答 3

2

那应该工作

/^[0-9]{1,3}(?:\,[0-9]{3})*(?:\.[0-9]{1,2})?$/
于 2013-09-13T10:50:51.637 回答
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 。

于 2013-09-13T10:50:19.170 回答
0

这应该工作

^(?:[1-9]\d*|0)?(?:\.\d+)?$
于 2013-09-13T11:01:57.650 回答