2012-09-04 21 views
2

我使用jQuery的下面包裹在我的元素每個字母在跨度:jQuery - 在<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>')); 
    }  
}); 

我會用這個來包裹號。我遇到的問題是,當我的號碼中有一個逗號(例如23,000)時,逗號沒有被包裹。

任何想法如何我可以得到逗號包裹在<span>也?

謝謝!

回答

3

逗號不在\w的子集中。您可以使用.以匹配所有字符:

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

+1 ....像那.... – thecodeparadox

1

\ W不包括逗號,試試這個:/([\ W \])/ G