简体   繁体   English

正则表达式,用于验证货币数字格式

[英]Regex for validating currency number format

I've got following formats, that are acceptable 我有以下可接受的格式

1200000,00
1200000.00
1,200,000.00
1 200 000.00
1 200 000,00
1 200 000,0000
-1 200 000.00

At the moment I was able to verify only ^-?\\\\d+$ , ^-?\\\\d+[\\\\,\\\\.]\\\\d{2}$ , ^-?\\\\d+[\\\\,\\\\.]\\\\d{2,}$ . 目前,我仅能验证^-?\\\\d+$^-?\\\\d+[\\\\,\\\\.]\\\\d{2}$^-?\\\\d+[\\\\,\\\\.]\\\\d{2,}$ Two last format are separate, so that I would know is rounding needed or not. 最后两种格式是分开的,因此我知道是否需要舍入。 All three format use gm flags to check string from start ^ to end $ . 这三种格式都使用gm标志来检查从开始^到结束$字符串。

Those regular expressions cover only first two elements in list. 这些正则表达式仅覆盖列表中的前两个元素。 Other elements, that use commas and spaces for thousand separation are not verified yet and I'm not sure how to achieve that. 使用逗号和空格进行千位分隔的其他元素尚未得到验证,我不确定如何实现。

Also there is a "beautifier" expression (\\\\d)(?=(\\\\d{3})+(?!\\\\d)) , that will take this 1200000,00 and turn it into 1 200 000,00 with such usage '1200000,00'.replace(('(\\\\d)(?=(\\\\d{3})+(?!\\\\d))', 'g'), '$1 ') . 还有一个“美化”表达式(\\\\d)(?=(\\\\d{3})+(?!\\\\d)) ,它将把这个1200000,00变成1 200 000,00使用这样的用法'1200000,00'.replace(('(\\\\d)(?=(\\\\d{3})+(?!\\\\d))', 'g'), '$1 ')

So question states, what would be a correct regular expression to validate such format 1 200 000.00 or 1,200,000.00 ? 因此问题提出,验证这种格式1 200 000.001,200,000.00的正确正则表达式是什么? Since I assume difference with \\s\\, could be easily done in same expression. 由于我假设与\\s\\,有所不同\\s\\,因此可以在同一表达式中轻松完成。

Thank you. 谢谢。

For validating the last two numbers, you can use the following: 要验证最后两个数字,可以使用以下命令:

^-?\d{1,3}(?:[\s,]\d{3})*(?:\.\d+)?$
  1  2         3     4      5

可视化

  1. Optional minus sign 可选减号
  2. 1..3 digits 1..3位数字
    Zero or more fragments that consist of 零个或多个由以下组成的片段
  3. comma or space 逗号或空格
  4. 3 digits 3位数
  5. optional fraction part consisting of a dot followed by 1 or more digits. 可选的分数部分,由点后跟1个或多个数字组成。

This doesn't directly solve the problem due to me misreading. 由于我读错了,这不能直接解决问题。 But it might still be useful to someone so I'll let it stay. 但这可能仍然对某人有用,所以我让它留下。

Stop trying to solve every problem with regex. 停止尝试使用正则表达式解决所有问题。 Regex is great when you have one or two very well defined strings. 当您有一个或两个定义良好的字符串时,正则表达式非常有用。 Not a million formats. 不是一百万种格式。

This can be solved with minimal regex. 这可以用最少的正则表达式解决。 Magic is in the bold part. 魔术在大胆的部分。

var numbers = [
    "1200000,00",
    "1200000.00",
    "1,200,000.00",
    "1 200 000.00",
    "1 200 000,00",
    "1 200 000,0000",
    "-1 200 000.00"
];

var parseWeirdNumber = function(numberString) { //Split numbers to parts. , . and space are all valid delimiters. var numberParts = numberString.split(/[.,\s]/); //Remove the last part. **This means that all input must have fraction!!** var fraction = numberParts.pop(); //Rejoin back without delimiters, and reapply the fraction. //parseFloat to convert to a number var number = parseFloat(numberParts.join('') + "." + fraction); return number; }

numbers = numbers.map(parseWeirdNumber);

console.log(numbers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM