简体   繁体   中英

Regex for multiple emails separated by commas with valid emails

I have regex to check multiple valid emails separated by commas, But I'm not able to update it to allow multiple trailing and leading whitespace. I should allow below pattern to match my regex.

abc@test.com,bcd@test.com, new@test.com,   hello@test.com   ,new@test.com // allow
abc@test.com bcd@test.comnew@test.com// don't allow

Regex which I've now.

^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$

The above regex is not allowing the pattern

abc@test.com,bcd@test.com, new@test.com,   hello@test.com   ,new@test.com

It allows only one white space before an after the comma. I want the regex to allow multiple white space before and after the comma

Why not first spiting the string:

function getValidEmailAddresses(strInput)
{
return strInput.split(',').map(p => p.trim())
               .filter(c=> c.test(/^(([^<>()\[\]\\.,;:\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,}))$/));

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM