简体   繁体   中英

How to set the count of characters in several groups of regular expression?

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])?)*[ ]*$/;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM