RegExp验证电子邮件地址 [英] RegExp to validate an email address

查看:95
本文介绍了RegExp验证电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我来到RegExp时,我正在阅读我的O''Reilly JavaScript The Definitive Guide

并认为我可以收紧我的JavaScript代码

检查是否有效电子邮件地址。为什么以下似乎不起作用:


var email_address =" Joe @ Schmoe" ;;

var email_regex = new RegExp(" ^(\\ \\\w +)(\ @)(\\\\ + +)(\。)(\\\\ + +)$");

var result = email_regex.exec(email_address );

alert(" result [1] = \"" + result [1] +" \" \ n" +

" result [2] = \"" + result [2] +" \" \ n" +

" result [3] = \"" ; +结果[3] +" \" \ n" +

" result [4] = \"" + result [4] +" \" ; \ n" +

" result [5] = \"" + result [5] +" \"");

生成的警报框包含:


结果[1] =" Joe"
结果[2] =" @"

结果[3] =" Schm"
结果[4] =" o"

结果t [5] =" e"


似乎\。并没有逃避。表示文字

期间。我做错了什么?

-

你怎么没有听说过那些笨拙的员工?无论如何,谁已经无视了?

I was reading my O''Reilly JavaScript The Definitive Guide when I came
across RegExp and thought I could tighten up my JavaScript code that
checks for a valid email address. Why does the following not appear to work:

var email_address = "Joe@Schmoe";
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)$");
var result = email_regex.exec (email_address);
alert (" result[1] = \"" + result[1] + "\"\n" +
" result[2] = \"" + result[2] + "\"\n" +
" result[3] = \"" + result[3] + "\"\n" +
" result[4] = \"" + result[4] + "\"\n" +
" result[5] = \"" + result[5] + "\"");

The resulting alert box contains:

result[1] = "Joe"
result[2] = "@"
result[3] = "Schm"
result[4] = "o"
result[5] = "e"

It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?
--
How come you don''t ever hear about gruntled employees? And who has been
dissing them anyhow?

推荐答案

");

var result = email_regex.exec(email_address);

alert(" result [1] = \"" + result [1] +" \" \ n" +

" result [2] = \"" + result [2] +" \" \ n" +

" ; result [3] = \"" + result [3] +" \" \\\
" +

" result [4] = \"" + result [4] +" \" \ n" +

" result [5] = \"" + result [5] +" \" );


生成的警报框包含:


result [1] =" Joe"

result [2] =" @"

result [3] =" Schm"

result [4] =" o"

结果[5] =" e"


似乎\。并没有逃避。表示文字

期间。我做错了什么?

-

你怎么没有听说过那些笨拙的员工?谁无论如何都要给他们消费?

");
var result = email_regex.exec (email_address);
alert (" result[1] = \"" + result[1] + "\"\n" +
" result[2] = \"" + result[2] + "\"\n" +
" result[3] = \"" + result[3] + "\"\n" +
" result[4] = \"" + result[4] + "\"\n" +
" result[5] = \"" + result[5] + "\"");

The resulting alert box contains:

result[1] = "Joe"
result[2] = "@"
result[3] = "Schm"
result[4] = "o"
result[5] = "e"

It appears that the "\." is not escaping the "." to mean the literal
period. What am I doing wrong?
--
How come you don''t ever hear about gruntled employees? And who has been
dissing them anyhow?


>似乎\。并没有逃避。表示

literal
> It appears that the "\." is not escaping the "." to mean the
literal
期间。我做错了什么?




你有\。当你打算把\\。在RegExp字符串中。



You have "\." when you meant to put "\\." in the RegExp string.


Andrew DeFaria写道:
Andrew DeFaria wrote:
[...]
var email_regex = new RegExp(" ^(\\\\ +)(\ @)(\\\\ +)(\。)(\\\\ + +)
[...]
var email_regex = new RegExp ("^(\\w+)(\@)(\\w+)(\.)(\\w+)


这篇关于RegExp验证电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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