我需要使用 JavaScript 验证具有特定域名以及是否包含子域的电子邮件。
abcdefgh@abc.com - valid
abcdefgh@subdomain.abc.com - valid
abcdefgh@abc.com.au - invalid
abcdefgh@subdomain.abc.com.au - invalid
abcdefgh@subdomain.subdomain.abc.com - invalid
abcdefgh@subdomain.subdomain.abc.com.au - invalid
我使用的reg exp。
^[a-zA-Z]{1,}+@[a-zA-Z]{1,}\.abc\.com$
没有返回想要的结果。有更好的方法吗?
最佳答案
将子域部分设置为可选。
^[a-zA-Z]+@(?:[a-zA-Z]+\.abc|\.abc)\.com$
或
^[a-zA-Z]+@(?:[a-zA-Z]+\.)?abc\.com$
关于javascript - 正则表达式验证具有特定域/子域的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383238/