从javascript中生成的文本中删除点

时间:2015-11-14 19:21:03

标签: javascript jquery regex

我找到了一个我需要的网站代码,但它并不是我想要的( change font size after decimal point)。

所以我有:

$.each($('.price'), function() {
  var price = $(this).html();
  $(this).html(price.replace(/(\D*)(\d*\.)(\d*)/, '<span style="font-size:16px;">$1</span><span style="font-size:22px;">$2</span><span style="font-size:14px;">$3</span>'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='price'>$ 270.30</div>
<div class='price'>eur 64.60</div>
<div class='price'>€ 290.12</div>
<div class='price'>dollars 240.50</div>

问题是我希望最终得到没有点的值,例如:$ 270 30

有什么想法吗?

1 个答案:

答案 0 :(得分:5)

  

问题是我希望最终得到没有点的值,例如:$ 270 30

然后从第二个捕获组中删除\.。改变这个:

/(\D*)(\d*\.)(\d*)/

对此:

/(\D*)(\d*)\.(\d*)/

更新示例:

$.each($('.price'), function() {
  var price = $(this).html();
  $(this).html(price.replace(/(\D*)(\d*)\.(\d*)/, '<span style="font-size:16px;">$1</span><span style="font-size:22px;">$2</span> <span style="font-size:14px;">$3</span>'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='price'>$ 270.30</div>
<div class='price'>eur 64.60</div>
<div class='price'>€ 290.12</div>
<div class='price'>dollars 240.50</div>