I have the following option text that I want to put into a variable plus remove all characters except the value after the £ then convert it to a number. In this case 28.00
<option value="294">Set of 2 Boots</option><option value="295">Set of 4 Boots[Add £28.00]</option>
This converts the selects options text correctly.
recal_var = jQuery('select').find('option:selected').text();
But this doesn't seem to work with the £ symbol. If i change everything to a $ symbol I have no problems
price_result = parseFloat(recal_var.split('[Add £')[1].slice(0,-1).replace(/,/g,''));
I have also tried these as well with no luck.
price_result = parseFloat(recal_var.split('[Add £')[1].slice(0,-1).replace(/,/g,''));
price_result = parseFloat(recal_var.split('[Add £')[1].slice(0,-1).replace(/,/g,''));
What I get in firebug is recal_var.split("[Add \uFFFD")[1] is undefined
page has this metatag, not sure if that makes a difference.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
var str = "Set of 4 Boots[Add £28.00]";
var costStr = str.match(/£([\d.]+)/);
if(costStr){
var cost = parseFloat(costStr[1]);
alert(cost);
}
Example: jsbin
EDIT ignoring the £ and relying on position at end:
var str = "Set of 4 Boots[Add £28.00]";
var costStr = str.match(/([\d.]+)\]$/);
if(costStr){
var cost = parseFloat(costStr[1]);
alert(cost);
}
To match comma
var str = "Set of 4 Boots[Add £28,133.00]";
var costStr = str.match(/([\d.,]+)\]$/);
if(costStr){
var cost = parseFloat(costStr[1].replace(/,/g,""));
alert(cost);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With