6

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

Justin
  • 329
  • 2
  • 9
  • 19

6 Answers6

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]
Engineer
  • 47,849
  • 12
  • 88
  • 91
  • This is actually the best answer. If you need to add the prices, you'd be better off converting to actual numbers, then reformatting. – Madara's Ghost May 18 '12 at 08:23
  • He wrote he need to grab the value from one element to another, I'm not sure he can parse the value. – gdoron May 18 '12 at 08:23
  • 1
    This converts a dollar amount to a numeric value. This is certainly useful to know how to do. However it won't help him find the dollar amounts in the first place. – Mark Byers May 18 '12 at 08:23
  • 1
    @gdoron It is not obvious ,what OP meant by 'adding to another' phrase. Your answer grabs the value, my answer converts currency to number for performing maths on it (specifically 'add' operation). – Engineer May 18 '12 at 08:33
  • @Engineer - This worked perfectly for what I was doing, thanks. – Justin May 18 '12 at 09:08
2

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

Community
  • 1
  • 1
gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Modify to something like this:

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

or this

 \$\d{1,3}(,\d{3})*(?:\.\d+)?
Dmitry
  • 1,263
  • 11
  • 15
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'));
Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

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

WildWex
  • 11
  • 1
0

I got this one:

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

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

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • actually this regexp is incorrect as it match all numbers, not only currency add (?<=\$) in the beggining – Dmitry May 18 '12 at 08:20
  • @Dmitry: I've stated in my answer I don't match currency. He might want to change his currency later on, and it will be best if he didn't figure that one out with RegEx. – Madara's Ghost May 18 '12 at 08:21
  • As I statted yuo regexp match "all number", which doesn't mean that it match only number part of currency, this for example will match in "The player under number 47 was bought for $100.00" string even 47, which obviously not a price. – Dmitry May 18 '12 at 10:30