检查字符串是否包含电子邮件地址? [英] Check if a string contains an email address?

查看:118
本文介绍了检查字符串是否包含电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查以验证给定字符串是否包含电子邮件地址。

How can I check to verify that a given string contains an email address.

电子邮件地址也会包含在很多其他文本中。

The email address would be included with a lot of other text, as well.

此外,不一定要严格验证电子邮件地址本身。更多,只是想确保 a@b.xyz 存在。

Also, not looking to necessarily strictly validate the email address itself. More so just wanting to make sure that a@b.xyz is present.

示例字符串:

Overall I liked the service, but had trouble using the widget generator.

Want more info? You can contact me at bob@example.org.

简单的javascript很好,但我确实碰巧使用jQuery,所以如果有某种帮助让这更容易的功能...去吧。

Plain javascript is fine, but I do happen to be using jQuery, so if there's some sort of helper function that makes this easier...go for it.

推荐答案

Debuggex示例

JsFiddle示例

function checkIfEmailInString(text) { 
    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,}))/;
    return re.test(text);
}

这篇关于检查字符串是否包含电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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