Probocop
Probocop

Reputation: 10552

jQuery - wrapping a comma in a <span>?

I am using the following jQuery to wrap each letter in my element in a span:

$('.num .stat').children().andSelf().contents().each(function() {
  if(this.nodeType == 3) {
    var $this = $(this);
    $this.replaceWith($this.text().replace(/(\w)/g, '<span class="s-$&">$&</span>'));
  }    
});

I will be wrapping numbers using this. The problem I'm having is that when there is a comma in my number (e.g. 23,000) the comma is not getting wrapped.

Any idea how I can get the comma's wrapped in a <span> also?

Thanks!

Upvotes: 2

Views: 254

Answers (2)

Ryan Townsend
Ryan Townsend

Reputation: 1015

\w doesn't include commas, try this: /([\w\,])/g

Upvotes: 1

VisioN
VisioN

Reputation: 145408

Comma is not in the subset of \w. You may use . in order to match all characters:

$this.text().replace(/(.)/g, '<span class="s-$&">$&</span>')

Upvotes: 3

Related Questions