faqts : Computers : Programming : Languages : JavaScript : Language Core : Regular Expressions

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 8 people (88%) answered Yes
Recently 7 of 8 people (88%) answered Yes

Entry

JavaScript: Search/Replace: Regular expression: Library: Which regular expression to extract e-mail?

Dec 8th, 2004 07:53
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 08 December 2004 - 05:05 pm -------------------

JavaScript: Search/Replace: Regular expression: Library: Which regular 
expression to extract e-mail?

---

/[A-Za-z0-9_\-.]+\@[A-Za-z0-9_\-.]+\.([A-Za-z][A-Za-z]{2,4})/

---
---

e.g.

--- cut here: begin --------------------------------------------------

<!-------------------------------------------------------------------->
<HTML>
<!-------------------------------------------------------------------->
<SCRIPT>
 alert( /[A-Za-z0-9_\-.]+\@[A-Za-z0-9_\-.]+\.([A-Za-z][A-Za-z]
{2,4})/.test( "vanessa.beau@yahoo.com" ) ); // gives true
</SCRIPT>
<!-------------------------------------------------------------------->
</HTML>
<!-------------------------------------------------------------------->

--- cut here: end ----------------------------------------------------

---

will extract all e-mail addresses with an extension of 2, 3 or 4
characters (and not more).

---

For example, it will extract and highlight any of the following:

 my.first.name-my.department@mycompany.bz

 my.first.name-my.department@mycompany.biz

 my.first.name-my.department@mycompany.info

---

Note:

Hereby the Backus Naur diagram used to create the above regular 
expression:


   +-------------<------+         +-------------<------+
   |                    |         |                    |
->-+->-[A-Za-z0-9_-.]->-+->-[@]->-+->-[A-Za-z0-9_-.]->-+--+
                                                          |
+----------------------------<----------------------------+
|
|
|  +->-[A-Za-z]-[A-Za-z]------------------->-+
|  |                                         |
|  |                                         |
+>-+->-[A-Za-z]-[A-Za-z]-[A-Za-z]---------->-+->-
   |                                         |
   |                                         |
   +->-[A-Za-z]-[A-Za-z]-[A-Za-z]-[A-Za-z]->-+

---
---

[book: see also: Heinle, Nick / Pe<n~>a Bill - Designing with 
JavaScript]

---
---

Internet: see also:

---

JavaScript: Regular expression: Simple: Create: How create regular 
expression in JavaScript?
http://www.faqts.com/knowledge_base/view.phtml/aid/32577/fid/597

---

How do I validate the format of a URL or email?
http://www.faqts.com/knowledge_base/view.phtml/aid/22443/fid/597

---

TSE: Search/Replace: Regular expression: Library: Email: Which regular 
expression to extract e-mail?
http://www.faqts.com/knowledge_base/view.phtml/aid/25674/fid/865

----------------------------------------------------------------------