Regex validation in java and java scipt for email address
We are using below regular expression to validate email address in java ^[w!#$'*+=?^/_~-]+(.[w!#$'*+=?^/_~-]+)*@([a-zA-Z0-9-]+.)+[AZ]{2,4}$
and it worked fine with invalid email address raju.rathi@gmail.com&^(*&^(*^2
but when I use the same regular expression in javascript , it doesn't work and failes with even valid email addresses . Please suggest what could be root cause of this mismatch?
for eg in javascript , I'm getting false value with below test conditional -
/^[w!#$'*+=?^/~-]+(.[w!#$'*+=?^/~-]+)*@([a-zA-Z0-9-]+.)+ [AZ]{2,4}$/.test("raju.rathi@gmail.com")
You need to convert to
in Javascript regex literal:
/^[w!#$'*+=?^/~-]+(.[w!#$'*+=?^/~-]+)*@([a-zA-Z0-9-]+.)+[A-Z]{2,4}$/i.test("raju.rathi@gmail.com")
Also many special regex characters like $
, +
, *
etc. don't need to be escaped inside character class hence I have removed unnecessary escaping from your character class.
试试这个,它适用于我。
/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$/.test('raju.rathi@gmail.com');
<html>
<body>
<script>
var flag = /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,6})$/.test('raju.rathi@gmail.com');
</script>
<h5><script>document.write(flag)</script></h5>
</body>
</html>
链接地址: http://www.djcxy.com/p/16526.html
上一篇: 正则表达式为多个电子邮件地址