我试图从两个数字中获得百分比。如果你在小提琴上闲逛,它会与第一个号码一起使用。对于第二个数字,我需要更多地修剪第一个数字,但不确定如何。
第一个号码有效。
第二个数字需要一个正则表达式,用于删除数字后面的空格和字符。
第三个需要一个正则表达式,删除数字之间的逗号,以及点之后的所有字符。
第四个需要一个正则表达式,删除数字前的字符,数字和点之间的逗号以及后面的所有字符
我如何编写它以便它适用于所有情况?无论我尝试什么,只需刹车一个或其他。
小提琴:
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 // 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 995:- // 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 // 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 // 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 :(得分:0)
更换除数字字符以外的所有字符
str.replace(new RegExp("[^0-9.]","gm"),"your string");
假设你有这个字符串:
var str ="dsfdsf7834hf3876gfergh"
然后,str.replace(new RegExp("[^0-9.]","gm"),"foo");
将返回"foofoofoofoofoofoo7834foofoo3876foofoofoofoofoofoo"
答案 1 :(得分:0)
尝试类似:
str.replace(/[\D]/g, '');
答案 2 :(得分: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);
});