我遇到了javascript split
方法的问题。我想要一些代码来“拆分”电子邮件列表。
example: test@test.comfish@fish.comnone@none.com
你是如何拆分的?
答案 0 :(得分:0)
无论编程语言如何,您都需要编写(创建)识别电子邮件的人工智能(因为没有模式)。
但是既然你问如何做到这一点,我认为你需要非常简单的解决方案。在这种情况下,拆分文本基于.com,.net,.org ... 这很容易做到,但它可能会产生很多无效的电子邮件。
更新:以下是简单解决方案的代码示例(请注意,这仅适用于以3个字母结尾的所有域名:.com,.net,.org,.biz .. 。):
var emails = "test@test.comfish@fish.comnone@none.com"
var emailsArray = new Array()
while (emails !== '')
{
//ensures that dot is searched after @ symbol (so it can find this email as well: test.test@test.com)
//adding 4 characters makes up for dot + TLD ('.com'.length === 4)
var endOfEmail = emails.indexOf('.', emails.indexOf('@')) + 4
var tmpEmail = emails.substring(0, endOfEmail)
emails = emails.substring(endOfEmail)
emailsArray.push(tmpEmail)
}
alert(emailsArray)
此代码当然有缺点:
但我相信它具有最佳的time_to_do_it / percent_of_valid_emails比率,因为它只需要非常少的时间。
答案 1 :(得分:0)
假设您有不同的域名,例如.com
,.net
等,并且不能只在.com
上拆分,并假设您的域名和收件人名称相同,就像每个域名一样你的三个例子,你可能会做这样的疯狂事情:
var emails = "test@test.comfish@fish.comnone@none.com"
// get the string between @ and . to get the domain name
var domain = emails.substring(emails.lastIndexOf("@")+1,emails.lastIndexOf("."));
// split the string on the index before "domain@"
var last_email = split_on(emails, emails.indexOf( domain + "@" ) );
function split_on(value, index) {
return value.substring(0, index) + "," + value.substring(index);
}
// this gives the first emails together and splits "none@none.com"
// I'd loop through repeating this sort of process but moving in the
// index of the length of the email, so that you split the inner emails too
alert(last_email);
>>> test@test.comfish@fish.com, none@none.com