Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaScript中使用正则表达式验证货币金额_Javascript_Regex_Currency - Fatal编程技术网

在JavaScript中使用正则表达式验证货币金额

在JavaScript中使用正则表达式验证货币金额,javascript,regex,currency,Javascript,Regex,Currency,可能重复: 如何在JavaScript中使用正则表达式验证货币金额 小数分隔符:, 十、百等分隔符: 模式:。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 有效金额示例: 1 1234 123456 1.234 123.456 1.234.567 1,23 12345,67 1234567,89 1.234,56 123.456,78 1.234.567,89 编辑

可能重复:

如何在JavaScript中使用正则表达式验证货币金额

小数分隔符:

十、百等分隔符:

模式:
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

有效金额示例:

1
1234
123456

1.234
123.456
1.234.567

1,23
12345,67
1234567,89

1.234,56
123.456,78
1.234.567,89
编辑


我忘了提到下面的模式也是有效的:
。仅根据您给出的标准,这就是我提出的

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

它是丑陋的,当你发现更多需要匹配的案例时,情况只会变得更糟。您最好找到并使用一些验证库,而不是自己尝试这样做,尤其是在一个正则表达式中

更新以反映增加的要求


再次更新以下评论

它将匹配
123.123123
(三个尾随数字,而不是两个),因为它将接受逗号或句点作为千和十进制分隔符。为了解决这个问题,我现在基本上把表达式翻了一倍;它要么用逗号作为分隔符,以句点作为基点来匹配整个对象,要么用句点作为分隔符,以逗号作为基点来匹配整个对象

明白我说的越来越乱是什么意思吗?(二)


下面是详细的解释:

(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    \.?        # optional separating period
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (.###) any number of times (including none at all)
  (?:,\d{2})?  # optionally followed by a decimal comma and exactly two digits
$)             # End of string.
|              # ...or...
(?:^           # beginning of string
  \d{1,3}      # one, two, or three digits
  (?:
    ,?         # optional separating comma
    \d{3}      # followed by exactly three digits
  )*           # repeat this subpattern (,###) any number of times (including none at all)
  (?:\.\d{2})? # optionally followed by a decimal perioda and exactly two digits
$)             # End of string.
让它看起来更复杂的一件事是所有的
?:
。通常,正则表达式也会捕获(返回匹配项)所有子模式。所有
?:
所做的就是不要费心捕捉子模式。因此,从技术上讲,如果将所有的
?:
都取出来,那么完整的内容仍然会匹配整个字符串,这看起来更清楚一些:

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


此外,它也是一个很好的资源。

除了(刚刚添加的?)123.45案例外,它可以处理上述所有内容:

function foo (s) { return s.match(/^\d{1,3}(?:\.?\d{3})*(?:,\d\d)?$/) }

是否需要处理多种分隔符格式?

这适用于所有示例:

/^(?:\d+(?:,\d{3})*(?:\.\d{2})?|\d+(?:\.\d{3})*(?:,\d{2})?)$/
作为详细的正则表达式(但JavaScript不支持):


“12345”和“12345.67”也有效吗?哦,天哪。。。它是否也需要验证数据在我接受你的答案之前,你能更新正则表达式使它不接受超过两个小数吗?示例:123.456,789@Diogo很好的呼叫,请查看我的更新。您可以像这样更新以下详细的正则表达式:@Diogo吗?让它看起来更复杂的一件事是所有的
?:
。通常,正则表达式也会捕获(返回匹配项)所有子模式。所有
?:
所做的就是不要费心捕捉子模式。因此,从技术上讲,如果将所有字符串都去掉,它仍然会匹配整个字符串,这看起来更清晰:
/(^\d{1,3}(\.?\d{3})*(,\d{2})$);(^\d{1,3}(,?\d{3})*(\.\d{2})$)/
+1用于解释正则表达式的每个部分,否则它们会灼伤你的眼睛。+1同意Barry的观点。谢谢你添加这个。我本打算在要求和我的答案最终确定之后这样做,但你抢先了我。
^              # Start of string
(?:            # Match either...
 \d+           # one or more digits
 (?:,\d{3})*   # optionally followed by comma-separated threes of digits
 (?:\.\d{2})?  # optionally followed by a decimal point and exactly two digits
|              # ...or...
 \d+           # one or more digits
 (?:\.\d{3})*  # optionally followed by point-separated threes of digits
 (?:,\d{2})?   # optionally followed by a decimal comma and exactly two digits
)              # End of alternation
$              # End of string.