0

I have this regular expression for valid email addresses which works well: ^(?=[\w\s])\s*[-+.'\w]+@[-.\w]+\.[-.\w]{2,}\s*$

However, users are sometimes entering an address like "john@gmail.co". I could exclude "co" from my regex, but I still want to allow "john@example.co.uk", see here.

I looked at this post but I don't know how to include that in the part [-.\w]{2,} of my current expression.

How can I alter my regular expression to disallow ".co" at the end of an email address?

Adam
  • 6,041
  • 36
  • 120
  • 208
  • A number of other nonalphabetic characters such as `*` and `&` are allowed in the local part. `\w` is imprecise because it allows underscore in the domain part. See https://stackoverflow.com/questions/2049502/what-characters-are-allowed-in-an-email-address – tripleee Sep 12 '21 at 08:02

1 Answers1

2

You could write the pattern without the lookahead at the start as:

^\s*[-+.'\w]+@\w+(?:[-.]\w+)*\.(?!co\s*$)[a-z]{2,}\s*$

Note that [-+.'\w] and \w limits the range of valid email addresses.

The pattern matches:

  • ^ Start of string
  • \s*[-+.'\w]+@ Match optional whitespace chars and repeat any listed chars in the character class until matching @
  • \w+(?:[-.]\w+)* After the @ start with matching 1+ word chars and optionally repeat either . or - and 1+ word chars
  • \.(?!co\s*$) Match a dot and use a negative lookahead asserting not co and optional whitespace chars until the end of strinng
  • [a-z]{2,}\s* Match 2 or more chars a-z and optional whitespace chars
  • $ End of string

See a Regex demo

If you don't want to allow matching leading whitespace chars, you can omit \s* from the start and the end of the pattern.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70