RFC2822 Standard compliant RegEx
Reference: https://datatracker.ietf.org/doc/html/rfc2822

Before returning:

  • We first convert it to a string using String as it works with null and undefined too.
  • We reconvert it to string so that our function is not prone to injections ( as I don't know where and how it's implemented )
Code
Diff
  • const parse = (value = '') => {
      // RFC2822 Standard compliant RegEx
      // https://datatracker.ietf.org/doc/html/rfc2822
      const emailValidatorExp = new RegExp(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
      
      // We first convert it to a string using String as it works with null and undefined too
      // We reconvert it to string so that our function is not prone to injections
      return emailValidatorExp.test(String(value).trim());
    }
    • const parse = (value = '') => {
    • // if valid email, return true, else return false
    • return true;
    • // RFC2822 Standard compliant RegEx
    • // https://datatracker.ietf.org/doc/html/rfc2822
    • const emailValidatorExp = new RegExp(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
    • // We first convert it to a string using String as it works with null and undefined too
    • // We reconvert it to string so that our function is not prone to injections
    • return emailValidatorExp.test(String(value).trim());
    • }