0

This javascript code tries to validate a string using regex saved in a pat variable. It gives false every time even though it is expected to give true.
What am I doing wrong? Thanks

isValid: (key, str) => {
  const pat = ValidationCol.findOne({'key': key}).regex; //=> ^[^@]+@[^@]+?\.[a-zA-Z]{2,6}$
  return (/pat/g.test(str));
}
0

1 Answer 1

1

If pat is a string, you should call new RegExp(pat, 'g').test(str). If RegExp, call pat.test(str). Your current code is checking if str contains the string 'pat'.

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.