Function removes css class names from element class attribute.
Supports regexp as a class name (thing I wait from jQuery for years).
Preserves order, leaves no duplicate or trailing spaces in className.
Regexp should be matched fully, not partially.
/foo-.*/ will match 'foo-bar' in 'foo-bar baz'
/foo-/ will not.
^ and $ means start and end of class name chunk, not of the whole className string
i (case-independent) modifier search is supported
m (multiline) modifier has no meaning
g (global) modifier is ignored: all search behaves as global
removeClass(o, c)
o required object whose className is changed
c required class name: regexp or string (or anything that coerces to string, excepting null and undefined)
Returns: nothing
@atk, it works incorrectly:
var foo; foo = 42, 34; foo // 42, not 34. Also, it relies onString.prototype.trimnot defined by ECMA 262-3.Fixed and shortened:
function(o,c,r){r='';o.className.replace(/\S+/g,function(x){(c.exec?(c.exec(x)||0)[0]:c)==x||(r+=(r&&' ')+x)});o.className=r}Also, I tested both versions in node.js. Array-based one is ≈15% faster (varies). I suppose, this happens because of function call overhead.