最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - javascript email regular expression - Stack Overflow

programmeradmin3浏览0评论

Can someone explain this regular expression to validate email.

var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

I need to know what does this independent elements do

"/^"  and "\"  and "\.\-" and "$"  //Please explain individually

Thanks in advance

Can someone explain this regular expression to validate email.

var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

I need to know what does this independent elements do

"/^"  and "\"  and "\.\-" and "$"  //Please explain individually

Thanks in advance

Share Improve this question edited Jun 3, 2011 at 2:11 Alan Moore 75.3k13 gold badges107 silver badges161 bronze badges asked Feb 10, 2011 at 13:46 Some Java GuySome Java Guy 5,11820 gold badges78 silver badges110 bronze badges 3
  • regular-expressions.info/reference.html – KJYe.Name Commented Feb 10, 2011 at 13:47
  • 3 That expression is broken and will reject many perfectly valid email addresses. – Quentin Commented Feb 10, 2011 at 13:48
  • possible duplicate of What is the best regular expression for validating email addresses? – Brad Mace Commented Jul 9, 2011 at 4:32
Add a ment  | 

3 Answers 3

Reset to default 5

Quick explanation

/

JavaScript regular expressions start with a / and end with another one. Everything in-between is a regular expression. After the second / there may be switches like g (global) and/or i (ignore case) ie. var rx = /.+/gi;)

^

Start of a text line (so nothing can be prepended before the email address). This also es in handy in multi-line texts.

\

Used to escape special characters. A dot/full-stop . is a special character and represents any single character but when presented as \. it means a dot/full-stop itself. Characters that need to escaped are usually used in regular expression syntax. (braces, curly braces, square brackets etc.) You'll know when you learn the syntax.

\.\-

Two escaped characters. Dot/full-stop and a minus/hyphen. So it means .-

$

End of line.

Learn regular expressions

They are one of the imperative things every developer should understand to some extent. At least some basic knowledge is mandatory.

Some resources

  • General regular expression syntax resource
    http://www.regular-expressions.info/
  • JavaScript related regular expressions
    https://developer.mozilla/en/Core_JavaScript_1.5_Guide/Regular_Expressions

/

The start of the expression

^

The start of the string (since it appears at the start of the expression)

\

Nothing outside the context of the character that follows it

\.\-

A full stop. A hyphen.

$

The end of the string

The other posters have done an excellent job at explaining this regex, but if your goal is to actually do e-mail validation in JavaScript, please check out this StackOverflow thread.

发布评论

评论列表(0)

  1. 暂无评论