I have a number that is generated in an X,XXX format (so it has commas). The numbers need to be styled (with a black background & white text) whereas the commas need to be as they are.
Anyway, I’m trying to add a span around each number in the string, but it doesn’t appear to be working.
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
var stylednumbers = parts.join(".");
var stylednumbers = stylednumbers.replace(/([0-9]+\.)/, '<span class="counternumber">$1</span>');
return stylednumbers;
}
I am a bit of a javascript novice (and avoiding jQuery as well, as it’s WordPress), but keen to get it working. Any ideas on where I am going wrong?
Any further information that you need I’ll be happy to provide.
I think you want your regexp to be
/([0-9]+)/g, otherwise only the last three digits of your number will get spanned.