JavaScript 中货币值的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11799539/
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-24 07:08:00  来源:igfitidea点击:

regex for money values in JavaScript

javascriptregex

提问by cakeforcerberus

Been out of the regexgame for a while. Trying to come up with something that will allow the user to enter a money value either with/without dollar sign or with/without commas. For example, all the of the following values should be valid:

离开regex游戏有一段时间了。试图想出一些东西,允许用户输入带/不带美元符号或带/不带逗号的货币值。例如,以下所有值都应有效:

5
5.1
5.10

500,000
500,000.1
500,000.10
0,000,000.50
etc....

Could someone please help me out?

有人可以帮我吗?

回答by Kip

This should work:

这应该有效:

isValid = str.search(/^$?[\d,]+(\.\d*)?$/) >= 0;

A little more strict with comma placement (would reject 3,2.10, for example):

对逗号放置更严格(例如拒绝 3,2.10):

isValid = str.search(/^$?\d+(,\d{3})*(\.\d*)?$/) >= 0;

To get a number out of it:

要从中获取一个数字:

if(isValid) {
  var num = Number(str.replace(/[$,]/g, ''));
  ...
}

回答by Larry Battle

I didn't Test Driven Developement, TDD, for this one using the Qunit framework.

我没有为此使用 Qunit 框架进行测试驱动开发,TDD。

TDD overview http://net.tutsplus.com/tutorials/javascript-ajax/test-driven-javascript-development-in-practice/

TDD 概述http://net.tutsplus.com/tutorials/javascript-ajax/test-driven-javascript-development-in-practice/

1st: Write tests.

第一:编写测试。

2nd: Watch tests fail.

第二:观察测试失败。

3rd: Make test pass.

第三:让测试通过。

4th: Refactor.

第四:重构。

var moneyTest_RE = /^$?\d+((,\d{3})+)?(\.\d+)?$/;
test("test money format for valid values", function () {
    var moneyArr = ["5","5.1","5.10","","500,000","500,000.1","500,000.10","0,000,000.50", "500,000,100" ];
    var i = moneyArr.length;

    while( i-- ){
        equal( moneyTest_RE.test( moneyArr[ i ] ), true, moneyArr[ i ] + " didn't match completely." );
    }
});
test("test money format for invalid values", function () {
    var moneyArr = ["5..","$.1",".5.10",".2.","50,0,000",",500,000.1","500,000,10,",",00,000,000.50", "500,000,10"];
    var i = moneyArr.length;

    while( i-- ){
        equal( moneyTest_RE.test( moneyArr[ i ] ), false, moneyArr[ i ] + " didn't match completely." );
    }
});

Here's one possible solution to your problem.

这是您问题的一种可能解决方案。

var moneyTest_RE = /^$?\d+((,\d{3})+)?(\.\d+)?$/;  

Demo here: http://jsfiddle.net/vpyV6/

演示在这里:http: //jsfiddle.net/vpyV6/

I forgot to refactor though.

不过我忘了重构。

回答by Riot Goes Woof

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

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

This one took a while, but I finally got something fully functional. It allows for cases such as 100.00, .35, $1.35, etc. While excluding entries with misplaced commas, too many numbers in between or before commas, or too many numbers after the decimal point.

这个花了一段时间,但我终于得到了一些功能齐全的东西。它允许诸如 100.00、.35、$1.35 等情况。同时排除带有错位逗号、逗号之间或之前的数字过多或小数点后的数字过多的条目。

You can play around with it here

你可以在这里玩它

回答by Himanshu Teotia

var currencyRegex = /^[$£]\d+(?:\.\d\d)*$/g;

Example: $10 or £10 0r 10but if you use simple 10this will be wrong

示例:$10 or £10 0r 10但是如果您使用 simple10这将是错误的

回答by user1034533

Perhaps this? http://refiddle.com/2tg

也许这个? http://refiddle.com/2tg

($?(:?\d+,?.?)+)

Also, http://refiddle.com/2ti; a longer version that doesn't match numbers like 123,45.4.3

另外,http://refiddle.com/2ti;与 123,45.4.3 等数字不匹配的较长版本

^($?(:?\d+,?)+(?:.?\d+)?)$