1

So I am trying to validate a string with contains first some text, then an email surrounded by angled brackets '<>', for example

'Test <postmaster@email.academy>'

I have been trying to solve this for quite some time, I want to know if there is a regex that will help me to validate that the email is always surrounded by angled brackets

var data = {
    from: 'Test <postmaster@email.com>',
};

var dataValidation = function (data){
    var fromEmailAddress = data.from.split(' '); //['Test','<postmaster@email.com>']
    /////code below doesn't work as I have angled brackets around the email
    var validEmail = /^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+.)+([A-Za-z0-9]{2,4}|museum)$/;
    if(validEmail.test(fromEmailAddress[1])){
        console.log(fromEmailAddress[1])
    }

};
Rajat Garg
  • 542
  • 2
  • 11
  • 26
Adam
  • 2,418
  • 3
  • 17
  • 22
  • 3
    So why not add these angle brackets to the regex? `/^<[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+.)+([A-Za-z0-9]{2,4}|museum)>$/.test("")`. Or even `/^<[\w.%+-]+@([A-Za-z0-9-]+.)+([A-Za-z0-9]{2,4}|museum)>$/.test("")` – Wiktor Stribiżew Jun 13 '16 at 10:51
  • also, if you will have problems with regs in further this is a very helpful service http://regexr.com/ – GONG Jun 13 '16 at 10:56
  • "([a-zA-z ]+)(<(?:([a-zA-z._%+-]+)@([a-zA-z]+)\\.([a-zA-z]{3,4}|museum))>)" try this – King Jun 13 '16 at 11:07
  • 3
    `[A-Za-z0-9]{2,4}|museum` is really poor for matching TLD's, see: http://www.iana.org/domains/root/db – Toto Jun 13 '16 at 11:22
  • 3
    @Toto — I'm amused at the special casing for museum while ignoring all the others. – Quentin Jun 13 '16 at 12:07
  • Or http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Wiktor Stribiżew Jun 13 '16 at 12:09

0 Answers0