Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove all characters up to £ and remove last character ]

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 &#163;')[1].slice(0,-1).replace(/,/g,''));

price_result = parseFloat(recal_var.split('[Add &pound;')[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" />
like image 868
user357034 Avatar asked Oct 24 '22 05:10

user357034


1 Answers

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);
}
like image 153
epascarello Avatar answered Oct 27 '22 10:10

epascarello