Matching all words except one

Say I have a sentence:

I am a good buy and bad boy too

How to select every word except boy in this sentence using regular expression ?


You can use negative look behind:

\w+\b(?<!\bboy)

Or negative look ahead since not all support negative look behind

(?!boy\b)\b\w+

You can read about negative look ahead here


Try:

\b(?!boy\b).*?\b

which means:

  • Zero width word break (\b)
  • That isn't followed by "boy" and another word break;
  • followed by any characters in a non-greedy way;
  • Up until another word break.

Note: the word break matches the start of the string, the end of the string and any transition from word (number, letter or underscore) to non-word character or vice versa.


/\b(?!boy)\S+/g