javascript 如何使用正则表达式作为货币
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10649064/
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
How to use regex for currency
提问by Justin
I need to grab a price from one element and add it to another.
我需要从一个元素中获取价格并将其添加到另一个元素中。
I am using this:
我正在使用这个:
$\d+(?:\.\d+)?
Which seems to work for $0.50
, $1.00
, $20.00
, $200.00
but I hit a brick wall on $1,000.00
and $10,000.00
这似乎适用于$0.50
, $1.00
, $20.00
,$200.00
但我撞到了砖墙$1,000.00
10,000.00 美元
(Unlikely $10,000.00
will ever be used).
(不太可能$10,000.00
会被使用)。
The comma is tripping me up.
逗号把我绊倒了。
** Edit **
** 编辑 **
I went away for an hour to come back to heaps of answers. Before I go through them I thought I'd clarify rather than answering all comments:
我离开了一个小时,回到了一堆答案。在我浏览它们之前,我想我会澄清而不是回答所有评论:
The platform being used auto generates the total value of items in a shopping cart. It gets rendered in a an element - this changes depending on whether a user is adding or removing items.
所使用的平台会自动生成购物车中商品的总价值。它呈现在一个元素中——这取决于用户是添加还是删除项目。
The value is unlikely to go into 10,000.00 because the product costs are not that high.
由于产品成本并不高,因此价值不太可能达到 10,000.00。
I am new to using the regex it took me long enough to get this far, hence the question.
我是使用正则表达式的新手,我花了很长时间才走到这一步,因此是这个问题。
Auto generated HTML:
自动生成的 HTML:
<span vertical="False" quote="False" id="catCartSummary">
<table cellspacing="0" class="cartSummaryTable">
<tbody>
<tr>
<td class="cartSummaryItem">3 item(s), Total: 5.00 <a href="#" class="cartSummaryLink">View Cart</a></td>
</tr>
</tbody>
</table>
</span>
I just need the $ value in this case $115.00 - But I need it to work for $1,000.00
在这种情况下,我只需要 $115.00 的 $ 值 - 但我需要它以 $1,000.00 的价格工作
回答by Engineer
Replace non-digit
and non-dot
simbols by ''
, then apply parseFloat
:
替换non-digit
和non-dot
符号为''
,然后应用parseFloat
:
var currencyValue = parseFloat(",000.50".replace(/[^\d\.]/g,''));
console.log( currencyValue ) ; //1000.5
UPDATE:If your 'HTML Auto generator' generates valid currency strings, then
更新:如果您的“HTML 自动生成器”生成有效的货币字符串,则
/$\S+/g
regexp is enough, for extracting all currencies:
regexp 足以提取所有货币:
var currencies = $('#cartSummaryItem').text().match(/$\S+/g); // ["5.00"]
Then you could convert them to numbers for performing maths on them:
然后你可以将它们转换为数字以对它们进行数学运算:
var currenciesAsNumbers = currencies.map( function(item){
return parseFloat( item.replace(/[^\d\.]/g,'') );
}); // [115]
回答by gdoron is supporting Monica
This is working for me:
这对我有用:
/$\d+(,\d+)*(?:\.\d+)?/g
I found an excellent regex that grabs every valid currency and customed it a bit to your needs:
我找到了一个优秀的正则表达式,它可以获取所有有效货币并根据您的需要对其进行定制:
/^$[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$/g
回答by Dmitry
Modify to something like this:
修改成这样:
$(?:\d{1,3},)*\d+(?:\.\d+)?
or this
或这个
$\d{1,3}(,\d{3})*(?:\.\d+)?
回答by WildWex
Stripping out $ currency symbol as well as the comma can be easily achieved with this RegEx using replace: [^.0-9]
使用此 RegEx 使用替换可以轻松去除 $ 货币符号和逗号: [^.0-9]
Demo available here: http://rubular.com/r/Gwkx62RtLa
此处提供演示:http: //rubular.com/r/Gwkx62RtLa
回答by Fabrizio Calderan
var re = /^\d{1,3}(\,\d{3})*(?:\.\d+)?$/;
console.log(re.test('0.50'));
console.log(re.test('1'));
console.log(re.test('20.50'));
console.log(re.test('200.50'));
console.log(re.test('2,000'));
console.log(re.test('2,000.50'));
console.log(re.test('20,000'));
console.log(re.test('20,000.00'));
console.log(re.test('1,000,000'));
console.log(re.test('1,000,000.50'));
回答by Madara's Ghost
I got this one:
我得到了这个:
^\d{1,3}(,\d{3})*(\.\d+)?$
Will match the number only, not including the currency symbol.
将仅匹配数字,不包括货币符号。