简体   繁体   English

具有ng-pattern的多封电子邮件的正则表达式

[英]Regular expression for multiple emails with ng-pattern

I am attempting to use ng-pattern to filter out most major email addresses. 我正在尝试使用ng-pattern过滤掉大多数主要的电子邮件地址。

This is my regular expression, which admittedly I am not very versed in. 这是我的正则表达式,诚然我不是很精通。

<input type="email" name="email" ng-model='email.userInput' 
placeholder="Please enter your work email" 
ng-pattern="/^[A-Z0-9._%+-]+@(yahoo\.com|gmail\.com|msn\.com|hotmail\.com|live\.com)/">

I simply want to fail on the emails listed above... just looking for a step in the right direction. 我只是想在上面列出的电子邮件中失败...只是在寻找正确方向的一步。

Change your alternating group of invalid email domains to a negative lookahead ( (?!...) ): 将无效电子邮件域的交替组更改为负前瞻( (?!...) ):

^[A-Z0-9._%+-]+@(?!yahoo\.com|gmail\.com|msn\.com|hotmail\.com|live\.com)

Demo 演示版


To elaborate, a lookaround is a zero-width assertion meaning it doesn't actually match any characters. 详细地说,环顾四周是零宽度的断言,这意味着它实际上与任何字符都不匹配。 Since this is a negative lookahead, as soon as we match the @ , we will check to make sure it is not followed by yahoo.com , etc. Because of how this works, we can actually simplify a lot of this (since [A-Z0-9._%+-]+ may not cover a wide range of emails, which `type="email" should validate for us). 由于这是一个负面的超前查询,因此,一旦我们匹配@ ,我们将检查以确保其后没有yahoo.com等。由于其工作方式,我们实际上可以简化很多操作(因为[A-Z0-9._%+-]+可能无法覆盖范围广泛的电子邮件,“ type =“ email”应该为我们验证了这些电子邮件)。 I would personally use something like this : 我个人会使用这样的东西

@(?!(?:yahoo|gmail|msn|hotmail|live)\.com|$)

This will only match the @ symbol as long as they are not followed by yahoo/gmail/etc, .com and the end of the string. 只要不带yahoo / gmail / etc, .com和字符串末尾,它们只会匹配@符号。

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

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