Use JavaScript to find email address in a string?
Use the match method with a regular expression to extract email from a string in JavaScript.
Simple RegEx to extract email address from string
1 |
/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/ |
Note: It will match not match 100% of the email patterns.
Example of JavaScript extract email from string
In the example, we created a function with regex /([a-zA-Z0-9._-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/ to extract email ids (address) from the long text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <script type='text/javascript'> // function for get email id function extractEmails ( text ){ } //test </script> </head> <body> </body> </html> |
Output:

Here’s the decomposition of the regex /<(.*)>/
:
/
and/
are mandatory to define a regex<
and>
simply matches the two<
and>
in your string()
parenthesis “capture” what you’re looking for. Here, they get the mail address inside..*
:.
means “any character”, and*
means “any number of times. Combined, it means “any character any number of times”, and that is inside<
and>
, which correspond to the place where the mail is.
Q: How to Extract all email addresses from bulk text using JS?
Answer: Here is HTML example code with easy approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html> <head> <script type='text/javascript'> "cook son" <[email protected]>, "Stark joe" <[email protected]>, "Ronal techno" <[email protected]>, "Boo ke" <[email protected]>, "aki same" <[email protected]>, "Praksh Tom" <[email protected]>,; function extractEmails (text) { } console.log(extractEmails(text).join('\n')); </script> </head> <body> <p id="emails"></p> </body> </html> |
Do comment if you have any doubts and suggestion with code in this topic.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version