使用javascript查找字符串中的电子邮件地址 [英] Use javascript to find email address in a string
问题描述
我要做的是从字符串中提取电子邮件地址 SomeName,First(First.SomeName@usa.mywebsite1.com)
What I am trying to do is to extract email address out of the string SomeName, First (First.SomeName@usa.mywebsite1.com)
以下是我已尝试过的代码:
Here is the code that I already tried:
var stringToSearchIn="SomeName, First (First.SomeName@usa.mywebsite1.com)";
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
var extractedEmail=re.exec(stringToSearchIn);
代码中的变量extractedEmail返回null。
The variable extractedEmail in the code returns a null.
推荐答案
我试过这个并且失败了
I tried this and failed
。 ..对于发生的事情并不是一个非常有用的描述。猜测,重新找不到匹配。一个明显的原因是你在这里使用的正则表达式只匹配 ONLY 包含一个电子邮件地址的字符串 - 从正则表达式中删除结束锚点(^,$),你会得到你的电子邮件地址。
...is not a very useful description of what happenned. At a guess, the re failed to find a match. An obvious cause for this is the regex you've used here will only match a string which ONLY contains an email address - remove the end anchors (^,$) from the regex and you'll get your email address.
var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
这篇关于使用javascript查找字符串中的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!