简体   繁体   English

带有javascript的电子邮件查找器正则表达式

[英]Email finder regex with javascript

I have this regex for matching email addresses in a string. 我有此正则表达式用于匹配字符串中的电子邮件地址。

[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,})

It is not the best regex in the world but good enought for my use. 它不是世界上最好的正则表达式,但足以供我使用。

When I am using this with javascript like that: 当我将其与javascript一起使用时:

var emails = string.match("[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,})");

I am matching only the first email address, which is normal because I am not using the g modifier. 我只匹配第一个电子邮件地址,这很正常,因为我没有使用g修饰符。

The problem is that when I am using the g modifier like that: 问题是当我像这样使用g修饰符时:

var emails = email[0].match("[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+(\.[a-zA-Z0-9,!#\$%&'\*\+/=\?\^_`\{\|}~-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,})g");

the var emails is null, so my match is not working. var emails为null,因此我的匹配不起作用。

If you can please help me with this. 如果可以的话,请帮我。

Thank you. 谢谢。

尝试:

var emails = yourString.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);

试试这个我在系统中的servlet验证过滤器中使用过它,我不知道它是否可以在javascript中工作

"^[a-zA-Z0-9._]+@[a-zA-Z0-9._]{3,}$"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM