2016-06-13 33 views
0

我试图从两个数字中获取百分比。如果你在小提琴上玩耍,它会与第一个数字一起工作。对于第二个数字,我需要多修整一个数字,但不确定如何。修剪除数字以外的所有字符

第一个数字的作品。

第二个数字需要一个正则表达式,用于删除数字后面的空格和字符。

第三个需要一个正则表达式来删除数字和点之后的所有字符之间的逗号。

第四个需要一个正则表达式的数字之前删除字符,后面

数字和点之间的所有字符之间的逗号我怎样写它,所以它适用于所有的情况?无论我尝试什么,只是刹车一个或其他人。

小提琴:

https://jsfiddle.net/28dL2fvp/5/

脚本:

$('.left').each(function() { 
var frstCol = parseInt($(this).find('em.price.product-card-price').text().trim().replace(/[€\.]/g, ''), 10); 
var seCol = parseInt($(this).find('em.price.product-card-price.before').text().trim(), 10);   
var result = (frstCol/seCol) * 100; 
$(this).find('a.pricebubble').text(parseInt(result)|| 0); 
}); 

HTML:

<div class="left"> 
<em class="price product-card-price"> 
    €1.019&nbsp; // should count as 1019 
    <span class="vatstatus">Exclusief BTW</span> 
</em> 
<em class="price product-card-price before"> 
    1519 
</em> 
<a class="pricebubble"></a> 
</div> 

<div class="left"> 
<em class="price product-card-price"> 
    5&nbsp;995:-&nbsp; // should count as 5995 
    <span class="vatstatus">Exclusief BTW</span> 
</em> 
<em class="price product-card-price before"> 
    987 
</em> 
<a class="pricebubble"></a> 
</div> 

<div class="left"> 
<em class="price product-card-price"> 
    11,823.00SEK&nbsp; // should count as 1183 
    <span class="vatstatus">Exclusief BTW</span> 
</em> 
<em class="price product-card-price before"> 
    1987 
</em> 
<a class="pricebubble"></a> 
</div> 

<div class="left"> 
<em class="price product-card-price"> 
    SEK11,823.00&nbsp; // should count as 1183 
    <span class="vatstatus">Exclusief BTW</span> 
</em> 
<em class="price product-card-price before"> 
    1987 
</em> 
<a class="pricebubble"></a> 
</div> 
+0

你想过滤哪个变量? – Roysh

回答

0

更换所有,但数字字符

str.replace(new RegExp("[^0-9.]","gm"),"your string"); 

假设你有这个字符串:

var str ="dsfdsf7834hf3876gfergh" 

然后,str.replace(new RegExp("[^0-9.]","gm"),"foo"); 将返回"foofoofoofoofoofoo7834foofoo3876foofoofoofoofoofoo"

0

试着这么做:

str.replace(/[\D]/g, ''); 
0

请试试这个:(https://jsfiddle.net/28dL2fvp/6/

$('.left').each(function() { 
    var frstCol = parseInt($(this).find('em.price.product-card-price').text().replace(/[^0-9.]/g, "").replace(/\.[0-9]*/, ""), 10); 
    var seCol = parseInt($(this).find('em.price.product-card-price.before').text().replace(/[^0-9.]/g, ""), 10);   
    var result = (frstCol/seCol) * 100; 
    console.log("frstCol: ", frstCol, ", seCol: ", seCol); 
    $(this).find('a.pricebubble').text(parseInt(result)|| 0); 
}); 
+0

嗨@Luc这个作品是他最后两个数字,但不是第一个:https://jsfiddle.net/28dL2fvp/7/(改变结果以显示负数) –

+0

这取决于您的分隔符。我只是认为1.019是1,而不是1019:D任何方式,删除'.replace(/ \。[0-9] * /,“”)',你会得到1019 – Luc

+0

Hi @Luc,我在你的和我的小提琴,似乎不工作? –