Probably a simple one but my knowledge of creating regular expressions is a little vague.
I’m trying to match any string followed by a comma and a space except if it is ‘Bair Hugger’ or ‘Fluid Warmer’
Here is what I have so far
var re_comma = new RegExp("\w+[^Bair Hugger|Fluid Warmer]" + ", ", "i");
Any ideas?
New answer
Regarding your example I’d say it is really easier to split the string and iterate over it:
Otherwise, your expression becomes something like this:
and you have to use a callback for the replacement to decide whether to remove the preceding
,or trailing,or not:The intention of this code is less clear. Maybe there is a simpler expression, but I think filtering the array is still cleaner.
Old answer: (doesn’t solve the problem at hand but provides information regarding regular expressions).
[]denotes a character class and will only match one character out of the ones you provided.[^Bair Hugger|Fluid Warmer]is the same as[^Bair Huge|FldWm].You could use a negative lookahead:
Note that you have to use
\\inside a string to produce one\. Otherwise,"\w"becomeswand is not a special character sequence anymore.You also have to anchor the expression.Update: As you mentioned you want to match any string before the comma, I decided to use
.instead of\w, to match any character.