I have a string variable, selectors
, holding class selectors: .class1,.class2,.class3
I'd like to selectively remove classes from that list depending on certain criteria (e.g. a user clicks button #2 and it removes .class2
from the list).
I'm having trouble coming up with the regular expression that allows for an optional comma at the front of the string to be removed. Here's what I've got:
var toRemove = '.class2';
var re = new RegExp(/\,\?/toRemove, 'gi');
newSelectors = selectors.replace(re, '');
I realize I'm probably screwing up trying to combine a pattern match with a variable, but I'm not entirely sure how to handle that.