最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use regex for currency - Stack Overflow

programmeradmin2浏览0评论

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

(Unlikely $10,000.00 will ever be used).

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.

I am new to using the regex it took me long enough to get this far, hence the question.

Auto generated 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

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

(Unlikely $10,000.00 will ever be used).

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.

I am new to using the regex it took me long enough to get this far, hence the question.

Auto generated 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

Share Improve this question edited May 18, 2012 at 8:48 Justin asked May 18, 2012 at 8:12 JustinJustin 3292 gold badges9 silver badges19 bronze badges 5
  • FYI regex is part of javascript, not specifically jQuery. – Rory McCrossan Commented May 18, 2012 at 8:13
  • 1 What should happen with something like $1.000,00? – Mark Byers Commented May 18, 2012 at 8:14
  • can't you add a coma in your regex ? – jbduzan Commented May 18, 2012 at 8:15
  • @jbduzan: Presumably not, otherwise I guess he wouldn't be asking how to do it. – Mark Byers Commented May 18, 2012 at 8:16
  • 1 You got two good answers, it depends on what you want, parse the value or grab the value. – gdoron Commented May 18, 2012 at 8:59
Add a comment  | 

6 Answers 6

Reset to default 11

Replace non-digit and non-dot simbols by '', then apply 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

/\$\S+/g

regexp is enough, for extracting all currencies:

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]

This is working for me:

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

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

The source of the regex

Modify to something like this:

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

or this

 \$\d{1,3}(,\d{3})*(?:\.\d+)?
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'));

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

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

I got this one:

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

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

发布评论

评论列表(0)

  1. 暂无评论