本文介绍了JavaScript正则表达式电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 此代码始终提示null,这意味着字符串与表达式不匹配。 var pattern =^ \w + @ [a-zA-Z _] +?\。[a-zA-Z] {2,3 } $; function isEmailAddress(str){ str [email protected]; alert(str.match(pattern)); 返回str.match(pattern); } 解决方案如果你将你的正则表达式定义为一个字符串,然后所有反斜杠都需要转义,所以你应该使用'\\\\'代替'\\\'。 或者,将其定义为正则表达式: var pattern = / ^ \w + @ [a-zA-Z _] +? \ [A-ZA-Z] {2,3} $ /。 BTW,请不要验证电子邮件地址客户端。无论如何,你的正则表达式太简单了,无法通过实现。 在这里看到真实的东西: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html This code is always alerting out "null", which means that the string does not match the expression.var pattern = "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$";function isEmailAddress(str) { str = "[email protected]"; alert(str.match(pattern)); return str.match(pattern);} 解决方案 If you define your regular expression as a string then all backslashes need to be escaped, so instead of '\w' you should have '\\w'.Alternatively, define it as a regular expression:var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;BTW, please don't validate email addresses on the client-side. Your regular expression is way too simple to pass for a solid implementation anyway.See the real thing here: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html 这篇关于JavaScript正则表达式电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 21:46