JavaScript中的电子邮件正则表达式验证,允许中间使用字符(#$%&*等)

时间:2018-07-13 14:21:39

标签: javascript regex validation email

电子邮件格式类似于: local_part @ domain_part .com

  
    

Local_part domain_part 不应以特殊字符(@#&()*,。/ {} <> ^%[]〜`!$ = \ |; ::?)包括连字符(-)

         

Local_part domain_part 只能在中间包含上述特殊字符,例如(#$%&*-等)。

         

local_part 的长度限制分别为 64 domain_part 的长度为 255 个字符。

         

domain_part 不能包含所有数字

  

2 个答案:

答案 0 :(得分:0)

因此,根据您的有限描述,您似乎在寻找like this?

以下是模式:^(?<localpart>[^-].*[^-])@(?<domainpart>[^-].*[^-])\..{2,3}$

它将它们保存到命名的捕获组中,并假定localpart和domainpart都至少包含2个字符(应该是两个字符)。不允许以-开头或结尾。

还有其他要求吗?还是可以满足要求?

答案 1 :(得分:0)

更新了7/27/2018

^(?![.!#$%&'*+/=?^_`{|}~-])(?:[a-zA-Z0-9]|(?:(?:([.])(?!\1)|[!#$%&'*+/=?^_`{|}~-]))(?!@)){1,64}@(?=.{1,255}$)(?!\d+$)[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])?)*$

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

 ^                             # BOS
 (?! [.!#$%&'*+/=?^_`{|}~-] )  # Local does not start with one of these

 (?:                           # Local
      [a-zA-Z0-9] 

   |  
      # Special chars
      # Local does not end with one of these

      (?:
           # Option 1
           (?:
                ( [.] )                       # (1), Not a consecutive special char (specific), add more
                (?! \1 )                      # 
             |                              # or,
                [!#$%&'*+/=?^_`{|}~-]         # One of these other special chars, remove from here
           )

           # Option 2
           # |  ( [.!#$%&'*+/=?^_`{|}~-] )     # (1) Not a consecutive same special char 
           #    (?! \1 )                        

           # Option 3
           # |  [.!#$%&'*+/=?^_`{|}~-]         # Not a consecutive any special char
           #    (?! [.!#$%&'*+/=?^_`{|}~-] )  

           # Option 4, Original
           #  |  [.!#$%&'*+/=?^_`{|}~-]        # Any special char is OK
           #     

      )
      (?! @ )

 ){1,64}                       # 1 to 64 local characters

 @ 
 (?= .{1,255} $ )              # 1 to 255 domain characters
 (?! \d+ $ )                   # Domain must not contain all numbers

 [a-zA-Z0-9]                   # Domain
 (?:
      [a-zA-Z0-9-]{0,61} 
      [a-zA-Z0-9] 
 )?
 (?:
      \. 
      [a-zA-Z0-9] 
      (?:
           [a-zA-Z0-9-]{0,61} 
           [a-zA-Z0-9] 
      )?
 )*
 $                             # EOS