2016-11-29 56 views
1

我想在@之前输入句点(“。”)时,现有的电子邮件正则表达式失败。Javascript正则表达式否定 - 电子邮件正则表达式的否定期

这是正则表达式我现在所拥有的:

^[a-zA-Z]+[a-zA-Z0-9.][email protected]$ 

这些应该通过:

[email protected] 
[email protected] 

但这些不应该:

[email protected] 
[email protected] 

第一种情况开始时期被处理但第二种情况不是。

+0

@ dan08不是,如果是方括号 - 那么它就失去了特殊的意义。 – vlaz

+0

真实!不知道。评论撤销。 – dan08

+0

@ dan08耶 - 大多数人物在方括号中失去了他们的特殊含义。最显着的不是'[]'(你仍然需要逃避那些),以及表示字符范围的'-'。但是,如果放置在开始或结束时,则将其视为破折号。 – vlaz

回答

0

我会尝试: ^ [A-ZA-Z] + [A-ZA-Z0-9] * [A-ZA-Z0-9] + @ domain.com $

+0

请注意,这在@之前至少需要两个字符,所以例如'a @ domain.com'会失败。这可能或可能不是你想要的。 –

+0

正确 - 我试图保持接近他原来的正则表达式,因为他似乎希望它以一封信开头。 – theTrueMikeBrown

+0

糟糕,我没有注意到它不能以数字开头。无论如何,这不是一个真正的批评,只是关于表达的确切含义/含义的一个说明。 –

0

试试这个正则表达式:^[\w.+-]*[^\W.]@domain\.com$

  • [\w.+-]*任何数目的字母数字字符,+-.
  • [^\W.]不是非字母数字字符或.(这意味着任何接受的字符但.
  • @domain\.com匹配任何字符匹配匹配电子邮件的其余部分,根据需要更改域名或使用@\w\.\w+来匹配大多数域名。 (匹配所有域更复杂,查看更多完整的电子邮件匹配示例正则表达式here
1

这应该在@符号前不需要两个或多个字符。

^[a-zA-Z][a-zA-Z0-9]*(?:\.+[a-zA-Z0-9]+)*@domain\.com$ 

下面是它的分解:

^     Make sure we start at the beginning of the string 
[a-zA-Z]   First character needs to be a letter 
[a-zA-Z0-9]*  ...possibly followed by any number of letters or numbers. 
(?:    Start a non-capturing group 
    \.+   Match any periods... 
    [a-zA-Z0-9]+ ...followed by at least one letter or number 
)*     The whole group can appear zero or more times, to 
        offset the + quantifiers inside. Otherwise the 
        period would be required 
@domain\.com$  Match the rest of the string. At this point, the 
        only periods we've allowed are followed by at 
        least one number or letter