Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse/trim email addresses from text

Similar to this question, but not sure how to implement in this case.

A trusted user (don't need to be concerned with validating input) is typing/pasting email addresses into a text field. On the blur event, I'd like to look at the text and clean up whatever he inputed (typically after copying and pasting a list of addresses from an email client).

"Bob Smith" <[email protected]>, [email protected], "John Doe"<[email protected]>

would be trimmed to:

[email protected], [email protected], [email protected]

like image 636
snumpy Avatar asked Aug 17 '11 19:08

snumpy


3 Answers

This regex should remove anything in double-quotes as well as < and > characters.

/".*?"|[<>]/

In Javascript, you might have something along these lines:

line.replace(/".*?"|[<>]/g, '');
like image 56
Mark Biek Avatar answered Nov 08 '22 10:11

Mark Biek


Valid email address can be very strange, so I'd suggest to not forbidding anything in that field otherwise may be well possible that your program is useless because your users will not be able to send email to valid email addresses.

To read the whole story see this blog post or go for the RFC yourself.

like image 30
6502 Avatar answered Nov 08 '22 11:11

6502


var emailList = userInput
    .replace(/[^,;]*.?</g, "")
    .replace(/>/g, "")
    .replace(/[,; ]{1,}/g, "\n")
    .replace(/[\n]{2,}/g, "\n")
    .split("\n")

This allows the email list to be provided in the following formats (including copy and paste email list from you Google To box):

"Bob Rob"<[email protected]>, [email protected]; [email protected] [email protected]

The email Ids can be separated by ,, ;, or newlines.

like image 30
Shiv Shankar Avatar answered Nov 08 '22 10:11

Shiv Shankar