Search code examples
nsregularexpression

How to match when letters aren't the same?


Suppose I want to match four-letter words whose first and last letters are not the same. What is a regex that will do this?

For example, I tried this: ^(.)..[^\1]$ but got no results.


Solution

  • Negative lookahead solves your problem.

    /^(.)..(?!\1).$/

    https://regex101.com/r/MvqXdE/1