I have a string $295.52 would like to split it and end up with the following:
<span>$</span>295.<span>52</span>
I tried split the digits after the comma, successfully, but now I cant get a grip on the first character. Here is my code:
$('.slprice').each(function () {
var text = $(this).text().split('.');
for (var i = 0, len = text.length; i < len; i++) {
text[i] = '<span class="pricefix-' + i + '">' + text[i] + '</span>';
}
$(this).html(text.join(''));
});
You can use a Regex for this, setting up groups to match the parts you'd like to split in to span
elements. Something like this:
var $container = $('#foo');
var re = /(\$)(\d+\.)(\d+)/;
$('.slprice').each(function () {
var matches = $(this).text().match(re);
$(this).html('<span>' + matches[1] + '</span>' + matches[2] + '<span>' + matches[1] + '</span>');
});