简体   繁体   English

JavaScript的电子邮件地址正则表达式

[英]Email address regular expression for JavaScript

I am trying to implement a regex for an email address where the first part of the email should not exceed 64 characters and the second part after the @ symbol should not exceed 253 characters and that what I did 我正在尝试为电子邮件地址实现正则表达式,其中电子邮件的第一部分不应超过64个字符,@符号后的第二部分不应超过253个字符,而我所做的

/^([A-Za-z0-9_\-\.]{1,64})+\@([A-Za-z0-9_\-\.]{1,253})+\.([A-Za-z]{2,4})$/

But even if the first part exceed 64 characters it is still a match which should not be the case, I am using this link to test my regex: https://regex101.com/ 但是,即使第一部分超过64个字符,也仍然是匹配项,不应该这样,我使用此链接来测试我的正则表达式: https : //regex101.com/

Can anyone please assist with this 谁能帮忙

There are too many quantifiers in the pattern: + after {1,64}) will repeat the parenthesized pattern 1 or more times, and it is not likely what you expect. 模式中的数量词过多: {1,64})之后的+会重复括号模式1次或多次,这不太可能达到您的期望。 Same with ([A-Za-z0-9_\\-\\.]{1,253})+ . ([A-Za-z0-9_\\-\\.]{1,253})+

You may use 您可以使用

/^[\w.-]{1,64}@(?!.{254})[\w.-]+\.[A-Za-z]{2,4}$/

Details 细节

  • ^ - start of string ^ -字符串开头
  • [\\w.-]{1,64} - 1 to 64 letters, digits, _ , . [\\w.-]{1,64} - 1〜64个字母,数字, _. or - chars -字符
  • @ - a @ char @ -一个@ char
  • (?!.{254}) - no 254 chars to the right is allowed (?!.{254}) -不允许在右边254个字符
  • [\\w.-]+ - 1+ letters, digits, _ , . [\\w.-]+ - 1+字母,数字, _. or - chars -字符
  • \\. - a dot -一个点
  • [A-Za-z]{2,4} - two, three or four ASCII letters [A-Za-z]{2,4} -两个,三个或四个ASCII字母
  • $ - end of string. $ -字符串结尾。

You are repeating the groups with the quantifier 1+ times. 您正在使用量词将组重复1次以上。

You could omit the quantifiers (and perhaps also the groups if you don't want to use them separately or referring to them) 您可以省略量词(如果不想单独使用或引用它们,也可以省略组)

Note that you don't have to escape the dot in the character class. 请注意,您不必在字符类中转义点。

^[A-Za-z0-9_\-.]{1,64}@[A-Za-z0-9_\-.]{1,253}\.[A-Za-z]{2,4}$

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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