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 )
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 falsereturn 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());
- }
var assert = require('assert'); describe("Solution", function () { it("Debe pasar con Yjimenezar001@gmail.com", function () { const email1 = "Yjimenezar001@gmail.com"; assert.equal(parse(email1), true); }); // assert.equal(parse(email1), false); // Deleted this test you wrote because // this should never be a problem, you // should rather trim the string yourself. // Make it more pleasant for the user // The user may do a wrong copy paste, a space in front / after it's fine it("Debe pasar con Yjimenezar001@gmail.com with white space at the end", function () { const email1 = "Yjimenezar001@gmail.com "; assert.equal(parse(email1), true); }); it("Debe falar con Yjimenezar001@gmail.com with white space inside", function () { const email1 = "Yjimenezar001 @gmail.com"; const email2 = "Yjimenezar001@gma il.com"; const email3 = "Yjimenezar001 @gmail.com"; const email4 = "Yjim enezar001 @gmail.com"; assert.equal(parse(email1), false); assert.equal(parse(email2), false); assert.equal(parse(email3), false); assert.equal(parse(email4), false); }); });
describe("Solution", function() {it("Debe pasar con Yjimenezar001@gmail.com", function() {- var assert = require('assert');
- describe("Solution", function () {
- it("Debe pasar con Yjimenezar001@gmail.com", function () {
- const email1 = "Yjimenezar001@gmail.com";
Test.assertEquals(true, parse(email1));- assert.equal(parse(email1), true);
- });
it("Debe fallar con Yjimenezar001@gmail.com with white space", function() {- // assert.equal(parse(email1), false);
- // Deleted this test you wrote because
- // this should never be a problem, you
- // should rather trim the string yourself.
- // Make it more pleasant for the user
- // The user may do a wrong copy paste, a space in front / after it's fine
- it("Debe pasar con Yjimenezar001@gmail.com with white space at the end", function () {
- const email1 = "Yjimenezar001@gmail.com ";
Test.assertEquals(false, parse(email1));- assert.equal(parse(email1), true);
- });
- it("Debe falar con Yjimenezar001@gmail.com with white space inside", function () {
- const email1 = "Yjimenezar001 @gmail.com";
- const email2 = "Yjimenezar001@gma il.com";
- const email3 = "Yjimenezar001 @gmail.com";
- const email4 = "Yjim enezar001 @gmail.com";
- assert.equal(parse(email1), false);
- assert.equal(parse(email2), false);
- assert.equal(parse(email3), false);
- assert.equal(parse(email4), false);
- });
- });