45

I'm having the this text below:

[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>

Here emails are seprated by , or ;. I want to extract all emails present above and store them in array. Is there any easy way using regex to get all emails directly?

Philip Kirkbride
  • 21,381
  • 38
  • 125
  • 225
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125

8 Answers8

106

Here's how you can approach this:

HTML

<p id="emails"></p>

JavaScript

var text = '[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]> datum eternus [email protected]';    

function extractEmails (text)
{
    return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
     
$("#emails").text(extractEmails(text).join('\n'));

Result

[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]

Source: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)

Demo 1 Here

Demo 2 Here using jQuery's each iterator function

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
14

You can use this regex:

var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g;

You can extract the e-mails like this:

('[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>').match(re);

//["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"]
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
13

Just an update to the accepted answer. This does not work for "plus" signs in the email address. GMAIL supports [email protected].

I've updated to:

return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
Nick Caruso
  • 477
  • 4
  • 10
3

The bellow function is RFC2822 compliant according to Regexr.com

ES5 :

var extract = function(value) {
   var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g;
   return value && value.match(reg);
}

ES6 :

const reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g
const extract = value => value && value.match(reg)

Regexr community source

Sebastien H.
  • 6,818
  • 2
  • 28
  • 36
2

You don't need jQuery for that; JavaScript itself supports regexes built-in.

Have a look at Regular Expression for more info on using regex with JavaScript.

Other than that, I think you'll find the exact answer to your question somewhere else on Stack Overflow - How to find out emails and names out of a string in javascript

Community
  • 1
  • 1
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
2
function GetEmailsFromString(input) {
  var ret = [];
  var email = /\"([^\"]+)\"\s+\<([^\>]+)\>/g

  var match;
  while (match = email.exec(input))
    ret.push({'name':match[1], 'email':match[2]})

  return ret;
}

var str = '"Name one" <[email protected]>, ..., "And so on" <[email protected]>'
var emails = GetEmailsFromString(str)

Source

Community
  • 1
  • 1
Johan
  • 35,120
  • 54
  • 178
  • 293
1
 const = regex = /\S+[a-z0-9]@[a-z0-9\.]+/img
"hello [email protected] how are you? do you know [email protected]?".match(regex)
saeid abdi
  • 57
  • 5
0

A bunch of the answer in here are including lower/capital letters [a-zA-Z] AND the insensitive regex flag i, which is nonsense.

  • i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]).
  • \d matches a digit (equivalent to [0-9])As domain extensions don't end with numeric characters).

As a result, combined with the \d token. we get a much more condenses and elegant sentence.

/[a-z\d._+-]+@[a-z\d._-]+/gi

Demo

let input = '[email protected], "assdsdf" <[email protected]>, "rodnsdfald ferdfnson" <[email protected]>, "Affdmdol Gondfgale" <[email protected]>, "truform techno" <[email protected]>, "NiTsdfeSh ThIdfsKaRe" <[email protected]>, "akasdfsh kasdfstla" <[email protected]>, "Bisdsdfamal Prakaasdsh" <[email protected]>,; "milisdfsfnd ansdfasdfnsftwar" <[email protected]>'

function get_email(string) {
  return string.match(/[a-z\d._+-]+@[a-z\d._-]+/gi)
};

$('#output').html(get_email(input).join('; '));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>
amarinediary
  • 4,930
  • 4
  • 27
  • 45