JQuery · February 13, 2012 0

JQuery: removing css classes by RegExp/Regular expression

html:

<div class=&quot;foo1 foo2 foo3 bar1 foo4&quot;>Sample Text</div>

javascript:

var regEx = /^foo/;
var elm = $("div");

var classes = elm.attr(‘class’).split(/s+/); //it will return foo1, foo2, foo3, foo4

for (var i = 0; i < classes.length; i++) { var className = classes[i]; if (className.match(regEx)) { elm.removeClass(className); } } [/sourcecode] or a simple regExp [sourcecode language="javascript"] var elm = $("div"); var regEx = /^foo[0-9]*/; elm[0].className = elm[0].className.replace(regEx, ''); [/sourcecode]