2012-07-08 31 views

回答

1

您可以使用JavaScript正則表達式循環,雖然這將返回組的數組匹配。您也可以使用replace功能與一組反向引用刪除color-

$('.change').click(function() { 
    $('.link').each(function(e) { 
     // find regex matching groups 
     var regex = /color-(\w+)/gi; 
     var match; 
     while (match = regex.exec(this.className)) { 
      alert(match[1]); 
     } 
     // remove color- 
     this.className = this.className.replace(/color-(\w+)/gi, "$1"); 
    }); 
}); 

http://jsfiddle.net/cYmxB/2/

1

使用捕獲組的顏色:

$('.change').click(function() { 
    $('.link').each(function(e) { 
     var pattern = /color-(\w+)/gi; 
     var match = pattern.exec(this.className); 
     var color = match[1]; 
     alert(color); 
    }); 
});​ 

http://jsfiddle.net/cYmxB/1/