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, h
There is also a solution which use the word boundary metacharacter \b:
foo.className.replace(/\bbar\b/g ,'');
This can suite somebody, but be aware the word boundary occurs also between a word character [A-Za-z0-9_]
and the dash -
character. Therefore a class name e.g. 'different-bar-class' would also be replaced resulting in 'different--class'. However, as opposed to the above solutions, the "\b
" solution doesn't remove the whitespace character \s
before the class name, which may be desired, so a string e.g. 'firstbar bar' will end up as 'firstbar '.