estevan gomez estevan gomez - 5 months ago 19x
jQuery Question

jQuery Find and Replace all instances of part of a classname

I have several div's with a classname that looks like this:


some have:


What I want to do is to search for whatever the number at the end of the class is and replace all of them to:


How can I do this using jQuery?


you can use "begin with" selector like this

$("[class^='col-md-']").removeClass (function (index, css) {
    return (css.match (/(^|\s)col-md-\S+/g) || []).join(' ');

removeClass function based on this answer


will find any element with a class that begins with col-md-. The removeClass function will than remove it. And finally col-md-6 will be added.