Crys
Crys

Reputation: 67

Remove dot from text generated in javascript

I found a code that i need for a site but it is not exactly what i want ( change font size after decimal point).

So i have:

$.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>

The problem is that i want to have the value without the dot in the end, like: $ 270 30

Any ideas?

Upvotes: 3

Views: 1063

Answers (1)

Josh Crozier
Josh Crozier

Reputation: 240928

The problem is that I want to have the value without the dot in the end, like: $ 270 30

Then remove \. from the second capture group. Change this:

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

To this:

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

Updated Example:

$.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>

Upvotes: 5

Related Questions