3

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?

1 Answer 1

5

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I don't understand so well the regex part so that's why i didn't get it from the start. Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.