Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  111] [ 6]  / answers: 1 / hits: 18715  / 12 Years ago, fri, may 18, 2012, 12:00: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


More From » jquery

 Answers
10

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]

[#85504] Wednesday, May 16, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradleymoisesy

Total Points: 121
Total Questions: 105
Total Answers: 95

Location: Nepal
Member since Mon, Jan 4, 2021
3 Years ago
bradleymoisesy questions
Wed, Dec 22, 21, 00:00, 2 Years ago
Tue, Jun 1, 21, 00:00, 3 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;