Email list filter
I'm not even sure how to ask the question or even how to sea开发者_如何转开发rch 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" <test@test.com>,"test2" <test2@test.com>,"test3" <test@test3.com>
I'm able to split up the emails by picking up the commas but not familiar with the < >
can anyone assist me? Thanks!
Here's a quickie version:
var textAreaVal = '"test" <test@test.com>,"test2" <test2@test.com>,"test3" <test@test3.com>';
// 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 = ["test@test.com", "test2@test.com", "test@test3.com"]
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 ",bob@smith.com," (without the brackets), but hopefully you get the idea.
精彩评论