Regex Match All Except a Specific Word, Character, or Pattern

Regex is great for finding specific patterns, but can also be useful to match everything except an unwanted pattern.

A regular expression that matches everything except a specific pattern or word makes use of a negative lookahead. Inside the negative lookahead, various unwanted words, characters, or regex patterns can be listed, separated by an OR character.

For example, here’s an expression that will match any input that does not contain the text “ignoreThis”.

/^(?!.*ignoreThis).*/Edit with Regexity

Note that you can replace the text ignoreThisEdit with Regexity above with just about any regular expression, including:

Before we dive into each of these, let’s first discuss how the whole thing works:

ALSO READ: Regex Match Everything After A Specific Character

How The Main Expression Works

To begin our expression, we first start by allowing everything to be matched. This is done by the dot symbol .Edit with Regexity which matches any character, followed by a zero-or-more quantifier *Edit with Regexity. This allows us to match zero or more of any character:

/.*/Edit with Regexity

Next, we add a negative lookahead, written in the form (?!abc)Edit with Regexity. The negative lookahead looks ahead into the string to see if the specified expression (abcEdit with Regexity in this case) is present. It work by only checking whether the abcEdit with Regexity expression is present, without actually matching or returning the expression.

/(?!abc).*/Edit with Regexity

Note that we place the negative lookahead at the start of the expression to ensure that it is validated before anything else is checked.

The expression above will now start from the first character in the string, checking every substring for abcEdit with Regexity, and won’t match if it finds this expression. However, upon validating the substring starting with the second character, bcEdit with Regexity, the test will fail since bcEdit with Regexity is not equal to abcEdit with Regexity. Therefore, the remainder of the string will be matched. To prevent this from happening, we need to provide a start-of-string anchor ^Edit with Regexity:

/^(?!abc).*/Edit with Regexity

This anchor forces the matched expression to start at the beginning of the string and ensures that no subsequent sub-strings can be matched.

Finally, this expression above will reject any string starting with abcEdit with Regexity but will accept any string that starts with a different character followed by abcEdit with Regexity. In other words, it will accept aabcEdit with Regexity or xabcEdit with Regexity.

To prevent this from happening, we need to provide an additional expression that will notice the characters at the start of the string, together with the unwanted expression. To do this, we need to add another dot character .Edit with Regexity and zero-or-more quantifier *Edit with Regexity that will notice zero-or-more characters in front of the unwanted expression.

/^(?!.*abc).*/Edit with Regexity

Notice that we place the .*Edit with Regexity inside the negative lookahead. If we placed it in front of the negative lookahead, the entire string will be matched before the negative lookahead is even checked.

And this completes the general expression required. We can now tweak it to suit specific use-cases.

Let’s look at some examples.

Match All Except a Specific Word

To match everything except a specific word, we simply enter the unwanted word inside the negative lookahead. The following expression will not match any string containing the word fooEdit with Regexity:

/^(?!.*foo).*/Edit with Regexity

We can list multiple unwanted words by separating them with the OR symbol |Edit with Regexity. The following expression will ignore strings that contain any of the words dollarEdit with Regexity, euroEdit with Regexity, or poundEdit with Regexity:

/^(?!.*(dollar|euro|pound)).*/Edit with Regexity

Notice that we need to enclose the list of unwanted words in round brackets ()Edit with Regexity for this to work correctly. If the round brackets are ignored, the .*Edit with Regexity at the front of the negative lookahead will work together with dollarEdit with Regexity but not with euroEdit with Regexity or poundEdit with Regexity, causing sentences that contain other characters before these unwanted words to be matched.

Match All Except a Specific Character

To match everything except a specific character, simply insert the character inside the negative lookahead. This expression will ignore any string containing an aEdit with Regexity:

/^(?!.*a).*/Edit with Regexity

If the character you want to exclude is a reserved character in regex (such as ?Edit with Regexity or *Edit with Regexity) you need to include a backslash \Edit with Regexity in front of the character to escape it, as shown:

/^(?!.*\?).*/Edit with Regexity

For a set of characters, one can include them in square brackets. Note that special characters inside square brackets don’t need to be escaped. The following expression will not match any string that contains a vowel:

/^(?!.*[aeiou]).*/Edit with Regexity

Match All Except a Specific Pattern

In addition to unwanted words or characters, one can specify a pattern that must be avoided in all matches. The pattern must be placed inside the negative lookahead:

For example, this expression will not match any string that contains three consecutive digits \dEdit with Regexity:

/^(?!.*\d{3}).*/Edit with Regexity

The following expression will not match any spelling of the word grey or gray:

/^(?!.*gr(e|a)y).*/Edit with Regexity

Match All Except a List of Patterns

Finally, patterns can be combined by enclosing them in parentheses ()Edit with Regexity and separating them using the OR symbol |Edit with Regexity.

The following expression will not match any string containing three consecutive digits \dEdit with Regexity, nor a string containing a vowel:

/^(?!.*(\d{3}|[aeiou])).*/Edit with Regexity

Lookahead Support

It should be noted that some programming languages does not support lookaheads in their regex implementations and will therefore not be able to run the expressions above.

Sources

The regular expressions on this page were adapted from solutions presented on Stack Overflow by Wiktor Stribiżew on this question and by Wayne Conrad on this question.

Benjamin

Founder, owner, and sole content creator on RegexLand. Enjoys programming, blogging, and teaching others how to do the same. Read more...

2 thoughts on “Regex Match All Except a Specific Word, Character, or Pattern”

  1. I have written the following middleware but this is not totally right

    userSchema.pre(/^find/, function (next) {
    this.find({ active: { $ne: false } });
    next();
    });

    How can I write all find functions (/^find/) except findOne in regular expression

    please help

    Reply
  2. [2022-06-15 12:50:38 +0000] [531] [INFO] (‘14.140.224.110’, 0) – “WebSocket /ws/my-notifications/18325/” [accepted]
    14.140.224.110:0 – “GET /14/workspace/workspaces/sales-opportunity-overview/ HTTP/1.1” 200

    I want a pattern which contains “WebSocket word ignore that logs

    Reply

Leave a Comment