Skip to content

Instantly share code, notes, and snippets.

@zaus
Created September 27, 2013 20:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaus/6734731 to your computer and use it in GitHub Desktop.
Save zaus/6734731 to your computer and use it in GitHub Desktop.
jQuery.removeClass companion -- strips out matching segments
$.fn.stripClass = function (partialMatch, endOrBegin) {
/// <summary>
/// The way removeClass should have been implemented -- accepts a partialMatch (like "btn-") to search on and remove
/// </summary>
/// <param name="partialMatch">the class partial to match against, like "btn-" to match "btn-danger btn-active" but not "btn"</param>
/// <param name="endOrBegin">omit for beginning match; provide a 'truthy' value to only find classes ending with match</param>
/// <returns type=""></returns>
var x = new RegExp((!endOrBegin ? "\\b" : "\\S+") + partialMatch + "\\S*", 'g');
// http://stackoverflow.com/a/2644364/1037948
this.attr('class', function (i, c) {
if (!c) return;
return c.replace(x, '');
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment