ReactJS - 如何修复电子邮件验证的正则表达式错误?
ReactJS - How to fix regex error for email validation?
在 React 应用程序中,我使用正则表达式来检查电子邮件格式。实际上它工作正常,但有一个 CodeQL
错误,如 This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '+'.
中/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")
}
在 React 应用程序中,我使用正则表达式来检查电子邮件格式。实际上它工作正常,但有一个 CodeQL
错误,如 This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '+'.
中/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")
}