I was doing some experiment with regex in my learning process.
Input is : I am ironman and I was batman and I will be superman
I want to match all words except the word batman
I tried [^(batman)]+
but it doesn't match characters a,b,m,n,t
anywhere in string
How can I achieve it?
Okay, enough of bullshit, here's the code:
var words = input.split(" ").filter(function(str){
return str.toLowerCase() !== "batman";
});
Several ways are possible:
with a negative lookahead assertion (?!...)
(not followed by):
\b(?!batman\b)\w+
with a capture group (you must take in account only the capture group 1):
\b(?:batman\b|(\w+))
Why your pattern doesn't work:
You wrote [^(batman)]
but a character class is only a collection of characters without order, you can't describe substrings inside it. It is the same than [^abmnt()]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With