Angular:具有模式的响应式表单验证未按预期工作

问题描述

我正在使用角度5和反应形式,并且我正在尝试使用以下正则表达式来验证电子邮件

^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

我已经在https://regexr.com/中测试了此正则表达式

enter image description here

以角反应形式使用相同的正则表达式-> validators.compose

enter image description here

enter image description here

失败。

enter image description here

解决方法

问题在于我如何使用电子邮件正则表达式,我们不应该将正则表达式包含在''中,而应该包含在//

emailPattern

/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

出于某种原因,我的项目正则表达式包含在“中”并且工作正常。

如果有人想深入了解,这里是沙盒链接 https://codesandbox.io/s/practical-monad-7p2y3?file=/src/app/app.component.ts