首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对于这个正则表达式练习,这是一个很好的解决方案吗?

对于这个正则表达式练习,这是一个很好的解决方案吗?
EN

Stack Overflow用户
提问于 2010-12-28 21:03:49
回答 1查看 177关注 0票数 2

问题是我必须从提供的文本中提取电子邮件地址。这是我的最终代码:

代码语言:javascript
复制
var a = "A towel, it says, is about the most massively useful thing an interstellar hitchhiker can have. Partly it has great practical value - you can wrap it around you dent@vogon.com for warmth as you bound across the cold moons of Jaglan Beta; you can lie on it on the brilliant marble-sanded beaches of Santraginus V, foo@bar.bar.com inhaling the heady sea vapours; you can sleep under it beneath the stars which shine so redly on the desert world of Kakrafoon; use it john.smith@blah.org to sail a mini raft down the slow heavy river Moth; wet it for use in hand-to- hand-combat; wrap it round your head to ward off glom@flop.net noxious fumes or to avoid the gaze of the Ravenous Bugblatter Beast of Traal (a mindboggingly stupid animal, it assumes that if you can't see it, it can't see you - daft as a bush, but very ravenous); you can wave your towel in emergencies as a distress signal, and of course dry yourself off with it if it still seems to be clean enough.";
var re = /[a-z0-9-_\.]+@[a-z0-9-_]+(?:\.[a-z0-9-_]+)+/gi;
var output = a.match(re);
alert(output);

通过使用这个正则表达式,我消除了电子邮件地址,如: hi@.email.com、hi@email.com.、hi@e..mail.com等。有没有更好的方法呢?

EN

回答 1

Stack Overflow用户

发布于 2010-12-28 21:56:54

也许是这样的:

代码语言:javascript
复制
[\w\.]+@[\w\.]+[\w\.]*\.\w{2,4}
  • [\w\.]+:第一部分至少一次,带或不带“点”
  • @:电子邮件必填
  • [\w\.]+:域名的第一部分(myhost)
  • [\w\.]*:域名的可选部分(my.host)
  • \. : 强制“点”
  • \w{2,4}:主机的最后一部分(.fr、.info、.tel)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4546257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档