Sunday, 29 September 2013

How to use JS variable in regex expression?

How to use JS variable in regex expression?

(Pure javascript only, please!)
I want to create a helper function that removes a single class from a div
with several classes. I'm imagining a function as follows:
function remove_class(div, klass) {
div.className = div.className.replace( /(?:^|\s)active(?!\S)/ , '' );
}
The class removed here is active. However, how can I remove any class
name, ie. one passed as the klass variable?

No comments:

Post a Comment