Honshi
Honshi

Reputation:

Email list filter

I'm not even sure how to ask the question or even how to search for it. I've browsed around and what i've got was some really hardcore regex

Basically, I have this textarea where I can input my list of emails like how google does it. Filter should be able to just ignore anything within a double quote and only picks up the email within < >

example: "test" <[email protected]>,"test2" <[email protected]>,"test3" <[email protected]>

I'm able to split up the emails by picking up the commas but not familiar with the < >

can anyone assist me? Thanks!

Upvotes: 0

Views: 137

Answers (1)

machineghost
machineghost

Reputation: 35806

Here's a quickie version:

var textAreaVal = '"test" <[email protected]>,"test2" <[email protected]>,"test3" <[email protected]>';
// Better hope you don't have any "Smith, Bob"-type bits if you do it this way...
var textAreaLines = textAreaVal.split(",");
// Gonna assume you have jQuery here 'cause raw JS loops annoy me ;-)
// (if not you can hopefully still get the idea)
var emails = [];
$$.each(textAreaLines, function(index, value) {
  var email = /<(.*?)>/.exec(value)[1];
  if (email) emails.push(email);
});
// emails =  ["[email protected]", "[email protected]", "[email protected]"]

The key is this line:

 var email = /<(.*?)>/.exec(value)[1];

which essentially says:

 var email =
 // set email to

 /<(.*?)>/
// define a regex that matches the minimal amount possible ("?")
// of any character (".") between a "<" and a ">"

 .exec(value)
 // run that regex against value (ie. a line of input)

 [1];
 // and use only the first matched group ([1])

You'll probably want to do a slightly more complex regex to account for any crazy input, ensure that there's a "@", or for people who just do ",[email protected]," (without the brackets), but hopefully you get the idea.

Upvotes: 1

Related Questions