问题描述
我正在使用酷小部件从gmail / homail / yahoo等导入电子邮件地址。小部件仍然是测试版,我猜这就是为什么它不允许大量配置。它实际上只是用以下数据填充textarea:
I am using a cool widget to import email addresses out of gmail/homail/yahoo etc. The widget is still beta and I guess thats why it does not allow a lot of configuration. It actually just fills a textarea with the following data:
命名一个< [email protected]>,名称二< ; foo @ domain.com>,等等< [email protected]>
所以我想知道是否有人可以提供帮助我写一个正则表达式或类似的东西,以将所有值从字符串中取出成一个数组。所需的格式为:
So I wondered if someone could help me write a regex or something like that to get all values out ofa string into an array. The desired format would be:
[{name:'Name one',email:'foo @ domain'},{name:'Name两个',电子邮件:'foo @ domain'},{name:'依此类推',电子邮件:'[email protected]'}]
我是一个完整的正则表达式菜鸟,我不知道如何在javascript中这样做。谢谢你的帮助!
I am a total regex noob and I have no clue on how to do that in javascript. Thanks for your help!
推荐答案
function getEmailsFromString(input) {
var ret = [];
var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g
var match;
while (match = email.exec(input))
ret.push({'name':match[1], 'email':match[2]})
return ret;
}
var str = '"Name one" <[email protected]>, ..., "And so on" <[email protected]>'
var emails = getEmailsFromString(str)
这篇关于如何在javascript中找出字符串中的电子邮件和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!