JavaScript正则表达式电子邮件验证 [英] JavaScript Regular Expression Email Validation
问题描述
此代码始终提示null
,这意味着字符串与表达式不匹配。
< pre class =lang-js prettyprint-override>
var pattern =^ \w + @ [a-zA-Z _] +?\。[a-zA-Z] {2,3 } $;
function isEmailAddress(str){
str =azamsharp@gmail.com;
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 = "azamsharp@gmail.com";
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正则表达式电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!