Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ReactJS-如何修复电子邮件验证的正则表达式错误?_Reactjs_Regex_Validation_Email_Eslint - Fatal编程技术网

ReactJS-如何修复电子邮件验证的正则表达式错误?

ReactJS-如何修复电子邮件验证的正则表达式错误?,reactjs,regex,validation,email,eslint,Reactjs,Regex,Validation,Email,Eslint,在react应用程序中,我使用正则表达式检查电子邮件格式。实际上,它工作正常,但是有一个CodeQL错误,比如正则表达式的这一部分可能会在包含许多重复'+'的字符串上导致指数回溯。 在/index.js中 const emailRegex = RegExp(/^[\w-_+]+([.-]?[\w-_+]+)*@[\w-_+]+([.-]?[\w-_+]+)*(\.[\w-_+]{2,})+$/) if (!emailRegex.test(email)) { setError("In

在react应用程序中,我使用正则表达式检查电子邮件格式。实际上,它工作正常,但是有一个
CodeQL
错误,比如
正则表达式的这一部分可能会在包含许多重复'+'的字符串上导致指数回溯。

在/index.js中

const emailRegex = RegExp(/^[\w-_+]+([.-]?[\w-_+]+)*@[\w-_+]+([.-]?[\w-_+]+)*(\.[\w-_+]{2,})+$/)
if (!emailRegex.test(email)) {
  setError("Invalid email")
}
如果我尝试添加
\
来修复,那么将得到
eslint
警告。请告诉我在这两种情况下都适用的答案。

我这样做了:

 var emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (!emailRegex .test(email)) {
        setError("Invalid email")
        }
var-emailRegex=/^([^()[\]\\,;:\s@\“]+(\.[^()[\]\,;:\s@\“]+)*)(\“+”)(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]}.[1,3}.[0-9]+[1,3}.];
如果(!emailRegex.test(电子邮件)){
设置错误(“无效电子邮件”)
}

不是这个问题的解决方案,但我建议现有库验证电子邮件地址。大概是这样的: