Created
September 27, 2013 20:31
-
-
Save zaus/6734731 to your computer and use it in GitHub Desktop.
jQuery.removeClass companion -- strips out matching segments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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