Shp*_*ord 6 javascript regex string jquery parsing
如何检查以验证给定字符串是否包含电子邮件地址.
电子邮件地址也将包含在许多其他文本中.
此外,不一定要严格验证电子邮件地址本身.更多,只是想确保它a@b.xyz存在.
示例字符串:
Overall I liked the service, but had trouble using the widget generator.
Want more info? You can contact me at bob@example.org.
Run Code Online (Sandbox Code Playgroud)
普通的javascript很好,但我确实碰巧使用了jQuery,所以如果有某种辅助函数可以让它变得更容易......那就去吧.
Kin*_*rog 13
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);
}
Run Code Online (Sandbox Code Playgroud)
你可以用这个:
var StrObj = "whatever, this is my email dave@gmail.com other text";
var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emailsArray != null && emailsArray.length) {
//has email
}
Run Code Online (Sandbox Code Playgroud)
如果需要,还可以从阵列中获取电子邮件地址.