Let's make a little plugin for this:
/**
* ReplaceClass
*
* @param regexp|substr The old classname you are looking for
* @param newSubStr|function The new classname
* @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
* @depends jQuery
* @author @abernier
*/
(function($) {
$.fn.replaceClass = function(oldClass, newClass){
return this.each(function(i, el){
$(el).attr('class', function(index, attr){
return attr.replace(oldClass, newClass);
});
});
};
})(jQuery);
eg:
$('<div class="foo bar"></div>').replaceClass('foo', 'bar');
# => [div.bar.bar]
or with regexp:
$('<div class="foo-25 bar"></div>').replaceClass(/foo-[0-9]{2}/, 'foo-100');
# => [div.foo-100.bar]