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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6190312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:38:52  来源:igfitidea点击:

Validate currency amount using regular expressions in JavaScript

javascriptregexcurrency

提问by Diogo Cardoso

Possible Duplicate:
What's a C# regular expression that'll validate currency, float or integer?

可能的重复:
什么是 C# 正则表达式,可以验证货币、浮点数或整数?

How can I validate currency amount using regular expressions in JavaScript?

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

Decimals separator:,

小数分隔符:,

Tens, hundreds, etc. separator:.

十、百等分隔符:.

Pattern:###.###.###,##

图案:###.###.###,##

Examples of valid amounts:

有效金额示例:

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

EDIT

编辑

I forgot to mention that the following pattern is also valid: ###,###,###.##

我忘了提到以下模式也是有效的: ###,###,###.##

回答by Wiseguy

Based solely on the criteria you gave, this is what I came up with.

仅根据您给出的标准,这就是我想出的。

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

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

http://refiddle.com/18u

http://refiddle.com/18u

It is ugly, and it will only get worse as you find more cases that need to be matched. You'd be well served to find and use some validation library rather than try to do this all yourself, especially not in a single regular expression.

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

Updated to reflect added requirements.

更新以反映增加的要求。



Updated again in regard to comment below.

关于下面的评论再次更新。

It would match 123.123,123(three trailing digits instead of two) because it would accept either comma or period as both the thousands and decimal separators. To fix that, I've now essentially doubled up the expression; either it matches the whole thing with commas for separators and a period as the radix point, or it matches the whole thing with periods for separators and a comma as the radix point.

它将匹配123.123,123(三个尾随数字而不是两个),因为它接受逗号或句点作为千位分隔符和小数分隔符。为了解决这个问题,我现在基本上把表达式加倍了;要么以逗号为分隔符,以句点为小数点匹配整个内容,要么以句点为分隔符,以逗号为小数点匹配整个内容。

See what I mean about it getting messier? (^_^)

明白我的意思是它变得更糟了吗?(^_^)



Here's the verbose explanation:

这是详细的解释:

(?:^           # 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.

One thing that makes it look more complicated is all the ?:in there. Normally a regular expression captures (returns matches for) all of the subpatterns too. All ?:does is say to not bother to capture the subpattern. So technically, the full thing would still match your entire string if you took all of the ?:out, which looks a bit clearer:

让它看起来更复杂的一件事就是里面的所有内容?:。通常,正则表达式也会捕获(返回匹配)所有子模式。所有?:的确是说刻意去捕捉子模式。所以从技术上讲,如果你把所有的东西都拿出?:来,完整的东西仍然会匹配你的整个字符串,这看起来更清晰一些:

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

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

Also, regular-expressions.infois a great resource.

此外,regular-expressions.info是一个很好的资源。

回答by Tim Pietzcker

This works for all your examples:

这适用于您的所有示例:

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

As a verbose regex (not supported in JavaScript, though):

作为一个冗长的正则表达式(虽然在 JavaScript 中不受支持):

^              # 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.

回答by Tamzin Blake

This handles everything above except for the (just added?) 123.45 case:

除了(刚刚添加?) 123.45 情况之外,这可以处理上述所有内容:

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

Do you need to handle multiple separator formats?

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