如何使用正则表达式货币

时间:2021-08-25 19:29:11

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: $115.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美元的价值$ 115.00 - 但我需要它以1,000.00美元的价格工作

6 个解决方案

#1


10  

Replace non-digit and non-dot simbols by '', then apply parseFloat:

用''替换非数字和非点符号,然后应用parseFloat:

var currencyValue = parseFloat("$1,000.50".replace(/[^\d\.]/g,''));

console.log( currencyValue ) ;   //1000.5

UPDATE: If your 'HTML Auto generator' generates valid currency strings, then

更新:如果您的'HTML Auto generator'生成有效的货币字符串,那么

/\$\S+/g

regexp is enough, for extracting all currencies:

regexp足以提取所有货币:

var currencies = $('#cartSummaryItem').text().match(/\$\S+/g);    // ["$115.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]

#2


2  

This is working for me:

这对我有用:

/\$\d+(,\d+)*(?:\.\d+)?/g

DEMO

DEMO

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

DEMO

DEMO

The source of the regex

正则表达式的来源

#3


1  

Modify to something like this:

修改为这样的东西:

\$(?:\d{1,3},)*\d+(?:\.\d+)?

or this

或这个

 \$\d{1,3}(,\d{3})*(?:\.\d+)?

#4


1  

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'));

#5


1  

Stripping out $ currency symbol as well as the comma can be easily achieved with this RegEx using replace: [^.0-9]

使用此版本的RegEx可以轻松实现剥离$ currency符号以及逗号:[^ .0-9]

Demo available here: http://rubular.com/r/Gwkx62RtLa

可在此处获得演示:http://rubular.com/r/Gwkx62RtLa

#6


0  

I got this one:

我得到了这个:

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

Will match the number only, not including the currency symbol.

将仅匹配数字,不包括货币符号。

#1


10  

Replace non-digit and non-dot simbols by '', then apply parseFloat:

用''替换非数字和非点符号,然后应用parseFloat:

var currencyValue = parseFloat("$1,000.50".replace(/[^\d\.]/g,''));

console.log( currencyValue ) ;   //1000.5

UPDATE: If your 'HTML Auto generator' generates valid currency strings, then

更新:如果您的'HTML Auto generator'生成有效的货币字符串,那么

/\$\S+/g

regexp is enough, for extracting all currencies:

regexp足以提取所有货币:

var currencies = $('#cartSummaryItem').text().match(/\$\S+/g);    // ["$115.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]

#2


2  

This is working for me:

这对我有用:

/\$\d+(,\d+)*(?:\.\d+)?/g

DEMO

DEMO

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

DEMO

DEMO

The source of the regex

正则表达式的来源

#3


1  

Modify to something like this:

修改为这样的东西:

\$(?:\d{1,3},)*\d+(?:\.\d+)?

or this

或这个

 \$\d{1,3}(,\d{3})*(?:\.\d+)?

#4


1  

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'));

#5


1  

Stripping out $ currency symbol as well as the comma can be easily achieved with this RegEx using replace: [^.0-9]

使用此版本的RegEx可以轻松实现剥离$ currency符号以及逗号:[^ .0-9]

Demo available here: http://rubular.com/r/Gwkx62RtLa

可在此处获得演示:http://rubular.com/r/Gwkx62RtLa

#6


0  

I got this one:

我得到了这个:

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

Will match the number only, not including the currency symbol.

将仅匹配数字,不包括货币符号。