开发者

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">S开发者_如何学Cet 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" />


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);
}


Is it possibly a charset mismatch? The meta tag says UTF-8, but what Content-Type is the page served with?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜