1

I have a regexp, which checks the email addresses.

And how can I set the max count of symbols of whole block after @.

export const EMAILREGEX =
/^[ ]*[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]{1,64}@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*[ ]*$/;
Alexandre Adereyko
  • 438
  • 2
  • 4
  • 12
  • _“I have a regexp, which checks the email addresses”_ - so far, so [_bad_](https://stackoverflow.com/a/48170419/10955263) … _“the max count of symbols of whole block after @”_ - a) why, b) what maximum value would you want to set it to? – 04FS Apr 25 '19 at 09:34

1 Answers1

1

You can use positive look ahead just after @ character in your regex to specify the min max length of part that follows @ character. Let's say you want it to be minimum 10 characters and maximum 20, then you can write (?=.{10,20}$) just after @ in your regex. Here is how your regex should look like,

/^[ ]*[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]{1,64}@(?=.{10,20}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*[ ]*$/;

Here, (?=) is called positive look ahead and .{10,20}$ means any character minimum 10 and maximum 20 followed by end of string signified by $

You can also refer to this post for getting familiar with how look arounds work.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36