I found the following regex from another Stack Overflow question: Change an element's class with JavaScript
And have used it in part of my script with success, however in another it seems to be failing.
I threw together a very minimalist test case on jsFiddle, and it is also failing:
HTML:
<div class="foo" id="foo">
hello
</div>
JS:
$(document).ready(function(){
foo = document.getElementById('foo');
foo.className += ' bar foobar';
alert(foo.className);
foo.className.replace( /(?:^|\s)bar(?!\S)/ , '' )
alert(foo.className);
})