Logo

How to negate specific word in regex?

To negate (or exclude) a specific word in a regular expression, you typically use negative lookahead or negative lookbehind. For example, if you want to match any line that does not contain the word "BAD", you can write:

^(?!.*BAD).*$
  • ^(?!.*BAD) ensures the line does not have BAD anywhere.
  • .*$ matches the rest of the line if that condition is satisfied.

Similarly, if you want to ensure that a certain position in the string does not match a given word, you can use a negative lookbehind (if your regex engine supports it). Below are some common patterns and explanations.

1. Negative Lookahead

A negative lookahead (?!...) asserts that what follows cannot match a certain pattern. For example:

(?!foo)bar

This matches the substring "bar" only if it’s not preceded immediately by "foo" in the same position. But be careful: this example is context-specific—(?!...) is applied at the same position as bar.

Match a string that does not contain a word at all

^(?!.*\bBAD\b).*$
  • ^ asserts start of the string/line.
  • (?!.*\bBAD\b) says “from this point, it’s not possible to find the sequence BAD as a separate word anywhere in the string.”
  • .*$ then matches the entire string if that condition holds true.

Match a position that does not allow a certain word next

foo(?!bar)
  • This matches "foo" only if not followed by "bar" at the next position.
  • Good for scenarios like “match foo not followed by bar.”

2. Negative Lookbehind

If your regex engine supports lookbehind (like many PCRE implementations, Python’s re, or .NET’s Regex—but not older JavaScript engines prior to ES2018), you can do something like:

(?<!foo)bar

This ensures the substring "bar" is matched only if not immediately preceded by "foo".

Example

(?<!\bBAD\b)word

Would match "word" if it’s not immediately preceded by BAD as a whole word. In other words, the text BADword should fail to match, but GOODword or word by itself would match.

3. Additional Notes

  1. Word Boundaries
    If you’re negating a specific word (like BAD), consider using word boundaries (\b) so you don’t accidentally exclude partial matches. For example, BAD inside BADLY might or might not be considered a “word,” depending on your needs.

  2. Performance Considerations
    Negative lookaheads/lookbehinds can be more expensive depending on how they’re used—especially if combined with expensive sub-patterns. For large-scale or complex tasks, think about partial solutions or alternative logic (e.g., splitting strings or using multiple passes).

  3. Engine Support

    • Negative lookahead (?!...) is widely supported.
    • Negative lookbehind (?<!...) is not in older JavaScript but is in modern browsers (ES2018+) and many other languages.
  4. Practical vs. Strict
    For more complicated negations (e.g., “match everything unless it’s exactly foo but allow partial overlaps…”), you may need to combine multiple lookarounds or use alternative strategies like checking substring existence before running the main pattern.

4. Example in JavaScript

If you want to match lines in a large text that do not contain BAD:

const text = "This line is good\nThis line is BAD\nThis line is nice."; const regex = /^(?!.*\bBAD\b).*$/gm; // The 'm' flag for multi-line, 'g' for multiple matches. const matches = text.match(regex) || []; console.log(matches); // Output might be: ["This line is good", "This line is nice."]
  • \bBAD\b ensures we match the separate word BAD.
  • If you want partial matches to count (like BADLY), remove the \b boundaries.

Bottom Line

To negate a specific word or pattern in a regex, negative lookaheads (?!...) or negative lookbehinds (?<!...) are typically the go-to. For instance:

  • ^(?!.*BAD).*$ ensures an entire line does not contain BAD.
  • foo(?!bar) matches “foo” not followed by “bar.”
  • (?<!foo)bar matches “bar” not preceded by “foo,” given your engine supports lookbehind.

Bonus: Enhance Your Regex & JavaScript Skills

Regular expressions are powerful, but so is having a strong JavaScript foundation. For deeper learning, consider these DesignGurus.io courses:

Looking for 1-on-1 feedback? Explore Mock Interviews:

Also, check out the DesignGurus.io YouTube channel for free tutorials on system design, coding patterns, and more.

Conclusion: Use negative lookahead (?!...) or negative lookbehind (?<!...) to exclude a specific word in your regex pattern.

CONTRIBUTOR
TechGrind