3

I am trying to validate a TextEdit in order to contains a truly email address. I have a regular expression to do that, but it validates more than one address, and I just want to validate only one address, no more.

Here you are the expression I have:

/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/

It validates something like this:

yulien.paz@cav.desoft.cu, wcampbell@cav.desoft.cu

I need take the second email address out of the TextEdit.

So, How can I do that??

Note: I am sorry about my English.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
Campbell
  • 39
  • 1
  • 2
  • 1
    It's broken. It doesn't allow email addresses in, for example, the .mobi, .info or .museum TLDs. Nor does it allow email addresses containing a plus character or capital letters. Throw this out. – Quentin Feb 20 '10 at 15:50
  • 1
    @Campbell: We have girls on stackoverflow as well :) – Daniel Vassallo Feb 20 '10 at 15:52
  • 1
    If you need to take the 2nd email address out of the TextEdit, can you not use split() and use the 1st result from the resulting array (starting at index 0 I guess ? – ccheneson Feb 20 '10 at 16:02
  • 1
    @Daniel Vassallo: If I'd known I would have combed my hair. – Andy E Feb 20 '10 at 20:03
  • Probably the most popular regex question on SO: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – ic3b3rg Jun 02 '11 at 21:26

3 Answers3

2

Here a great library of regular expressions

http://regexlib.com/Search.aspx?k=email

And tester of regexp.

This is a sample http://regexlib.com/REDetails.aspx?regexp_id=88

^([\w-.]+)@(([([0-9]{1,3}.){3}[0-9]{1,3}])|(([\w-]+.)+)([a-zA-Z]{2,4}))$

elgoya
  • 101
  • 1
  • 4
1

In order to find email address inside a paragraph, none of the the above works. Instead you need to look for word boundary, ex.

\b[_\S\d-]+@[_\S\d-]+\.[\S]{2,3}\b

And then use /g if needed.

windmaomao
  • 7,120
  • 2
  • 32
  • 36
-3

Try this

/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/
streetparade
  • 32,000
  • 37
  • 101
  • 123
  • This regex will not allow for email addresses that contain a + sign. According to RFC 2822 (http://tools.ietf.org/html/rfc2822), the following non-alphanumeric characters are valid in an e-mail address: ! # $ % & ' * + - / = ? ^ _ – Joe Lencioni Feb 20 '10 at 15:53
  • Hmm.. ok but it would be much more longer than the above. – streetparade Feb 20 '10 at 15:55
  • 1
    Oh yes. Very much longer. http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html ...that's why you don't ‘validate’ e-mail addresses with regex. – bobince Feb 20 '10 at 19:18