如何在javascript中找出字符串中的电子邮件和名称 [英] How to find out emails and names out of a string in javascript

查看:102
本文介绍了如何在javascript中找出字符串中的电子邮件和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用酷小部件从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:

命名一个< foo@domain.com>,名称二< ; foo @ domain.com>,等等< andsoon@gmx.net>

所以我想知道是否有人可以提供帮助我写一个正则表达式或类似的东西,以将所有值从字符串中取出成一个数组。所需的格式为:

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:'依此类推',电子邮件:'andsoon@gmx.net'}]

我是一个完整的正则表达式菜鸟,我不知道如何在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" <foo@domain.com>, ..., "And so on" <andsoon@gmx.net>'
var emails = getEmailsFromString(str)

这篇关于如何在javascript中找出字符串中的电子邮件和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆