Skip to content Skip to sidebar Skip to footer

Remove Matching Class With Regex

I want to remove matching class like type-one before adding new class like type-two HTML :
OneTwo

Solution 1:

c.match(/type-[a-z]{0,5}/) is returning an array with one element.

You need to specify an index, i.e. c.match(/type-[a-z]{0,5}/)[0], but first you should check that a match as occurred, otherwise an error will be thrown.

$(this).parents(".demo").removeClass(function(i, c){
    var m = c.match(/type-[a-z]{0,5}/);
    return m ? m[0] : m
})

Solution 2:

What about this:

$(document).on("click" ,".demo > span", function(){
    $(this).addClass("active").siblings().removeClass("active");
    $(this).parents(".demo").removeClass().addClass("demo type-" + $(this).text().toLowerCase());
});

Here we remove all class names including demo and then reset it again with new type-xxx class.

Post a Comment for "Remove Matching Class With Regex"