Reputation: 1501
I have a data set with monetary values like "$28.2 mn" and "€ 19 bn". For formatting I need to turn this generated content:
<p>$28.2 mn</p>
Into this:
<p><span class="currency">$</span>28.2 <span class="denomination">mn</span></p>
I need to do this with Javascript/jQuery after the page has loaded.
The data may or may not have spaces after the currency symbol. There may or may not be spaces between the number and the denomination. The numbers sometimes are whole and sometimes have a a decimal value.
I have played with some regex but this is beyond my skill set. If anyone has any thoughts it would be much appreciated.
Upvotes: 0
Views: 715
Reputation: 1501
I was able to combine the regex expression left by NullUserException with the other answer submitted to come up with the solution:
$('p').html(function(i, c){
var cur = $.trim( c );
cur = cur.replace(/([^\d.,]+)\s*([\d.,]+)\s*([^\d.,]+)/g,
'<span class="currency">$1</span> $2 <span class="denomination">$3</span>');
return cur;
})
Upvotes: 1