【问题标题】:Javascript regex negation - Negate period on email regexJavascript 正则表达式否定 - 电子邮件正则表达式的否定句点
【发布时间】:2017-04-14 02:46:35
【问题描述】:

我希望现有的电子邮件正则表达式在 @ 之前输入句点(“.”)时失败。

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

^[a-zA-Z]+[a-zA-Z0-9.]+@domain.com$

这些应该通过:

test.a@domain.com
a.test@domain.com

但这些不应该:

.test@domain.com
test.@domain.com

以句号开头的第一种情况被处理,但第二种情况不被处理。

【问题讨论】:

  • @dan08 如果它是方括号,则不是 - 那么它就失去了它的特殊含义。
  • 真的!不知道。评论已撤销。
  • @dan08 是的 - 大多数字符在方括号中失去了它们的特殊含义。最值得注意的是[](您仍然需要转义这些),以及表示字符范围的-。但是,如果放在开头或结尾,则将其视为破折号。

标签: javascript regex email regex-negation


【解决方案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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 请注意,这需要在 @ 之前至少两个字符,因此例如 a@domain.com 会失败。这可能是也可能不是你想要的。
    • 是的 - 我试图让它接近他原来的正则表达式,因为他似乎希望它以字母开头。
    • 糟糕,我没有注意到它也不能以数字开头。无论如何,这并不是真正的批评,只是对表达的确切含义/含义的说明。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多