<p>I have a div with <code>id="a"</code> that may have any number of classes attached to it, from several groups. Each group has a specific prefix. In the javascript, I don't know which class from the group is on the div. I want to be able to clear all classes with a given prefix and then add a new one. If I want to remove all of the classes that begin with "bg", how do I do that? Something like this, but that actually works:</p>

<pre><code>$("#a").removeClass("bg*");
</code></pre>

You don't need any jQuery specific code to handle this.  Just use a RegExp to replace them:
```
```
$("#a").className = $("#a").className.replace(/\bbg.*?\b/g, '');
```
```
You can modify this to support any prefix but the faster method is above as the RegExp will be compiled only once:
```
```
function removeClassByPrefix(el, prefix) {
    var regx = new RegExp('\\b' + prefix + '.*?\\b', 'g');
    el.className = el.className.replace(regx, '');
    return el;
}
```
```