1

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Some Java Guy
  • 4,992
  • 19
  • 71
  • 108
  • http://www.regular-expressions.info/reference.html – KJYe.Name Feb 10 '11 at 13:47
  • 3
    That expression is broken and will reject many perfectly valid email addresses. – Quentin Feb 10 '11 at 13:48
  • possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – Brad Mace Jul 09 '11 at 04:32

3 Answers3

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 comes 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

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • @Robert: So my regex to allow a hyphen in the middle of the number would look like this ? var numericExpression_and_hyphen_in_middle = /^[0-9]+\-[0-9]+$/; – Some Java Guy Feb 10 '11 at 14:01
  • @oneofthelions: This will **force** a hyphen, not allow it. If you'd like to allow it, it should be followed by a question mark: `/^[0-9]+-?[0-9]+$/`. And it also doesn't mean that it will be right in the middle. At least one digit will be proceeding it and at least one succeeding. But there may not be equal number of digits on each side. – Robert Koritnik Feb 10 '11 at 14:04
  • @Robert: That is what I want to force the hyphen in middle. Now if I want at least one character after hyphen and at two would look like this?`/^[0-9]+\-[0-9]{1,2}+$/` – Some Java Guy Feb 10 '11 at 14:09
  • @oneofthelions: Oh and BTW: A hyphen doesn't need to be escaped as long as it's not part of a range definition in a set as in `[0-9]`. But even there it can be used freely when it can't make a conflict as in `[0-9a-z.+-]` All of these in a set are meant as literals. – Robert Koritnik Feb 10 '11 at 14:09
  • @oneofthelions: Almost. Remove the trailing `+` at the end. As long as you want these *characters* to be digits and not any characters. `/^[0-9]+-?[0-9]{1,2}$/`. **AND** if you do talk about digits, these two are equal: `[0-9]` = `\d`. So it can be shortened to: `/^\d+-?\d{1,2}$/` – Robert Koritnik Feb 10 '11 at 14:12
  • @Robert: Thanks, so my regex would look like this `/^[0-9]+-[0-9]{1,2}$/` I removed the `?` as I want to force the hyphen. – Some Java Guy Feb 10 '11 at 14:14
  • @oneofthelions: Yes. And replace those number ranges with digit literal `\d`. – Robert Koritnik Feb 10 '11 at 14:16
1

/

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

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.

Community
  • 1
  • 1
Tony Miller
  • 9,059
  • 2
  • 27
  • 46