Skip to content

Instantly share code, notes, and snippets.

@tremby
Last active December 24, 2015 13:19
Show Gist options
  • Save tremby/6804433 to your computer and use it in GitHub Desktop.
Save tremby/6804433 to your computer and use it in GitHub Desktop.
Jquery plugins to remove classes from elements by regex and to test if at least one element has a class by regex (same behaviour as .hasClass)
$.fn.removeClassRegex = function(regex) {
return this.removeClass(function(index, classes) {
return classes.split(/\s+/).filter(function(c) {
return regex.test(c);
}).join(' ')
});
};
$.fn.hasClassRegex = function(regex) {
var has = false;
this.each(function() {
var classes = $(this).attr('class');
if (classes == null) {
return;
}
$.each(classes.split(/\s+/), function() {
if (regex.test(this)) {
has = true;
return false;
}
});
if (has) {
return false;
}
});
return has;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment