1

In react app, I am using regex to check email format. Actually its working fine but there is a CodeQL error like This part of the regular expression may cause exponential backtracking on strings containing many repetitions of '+'.

In /index.js,

const emailRegex = RegExp(/^[\w-_+]+([.-]?[\w-_+]+)*@[\w-_+]+([.-]?[\w-_+]+)*(\.[\w-_+]{2,})+$/)
if (!emailRegex.test(email)) {
  setError("Invalid email")
}

If I try to add \ to fix then will get an eslint warnings. Please suggest me the answer which works in both cases.

1
  • Not a solution to this problem, but I would suggest existing library to validate email address. Something like this: npmjs.com/package/email-validator Commented Jan 4, 2021 at 6:39

1 Answer 1

1

I did that:

 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")
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.