删除最多£的所有字符并删除最后一个字符]

时间:2021-11-15 20:24:44

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

我有以下选项文本,我想放入一个变量加上删除所有字符,除了£之后的值,然后将其转换为数字。在这种情况下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

我在firebug中得到的是recal_var.split(“[Add \ uFFFD”] [1]未定义

page has this metatag, not sure if that makes a difference.

页面有这个metatag,不知道这是否有所作为。

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

2 个解决方案

#1


3  

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

#2


0  

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

它可能是一个charset不匹配?元标记表示UTF-8,但Content-Type是哪个页面?

#1


3  

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

#2


0  

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

它可能是一个charset不匹配?元标记表示UTF-8,但Content-Type是哪个页面?