Wyodrębnij wszystkie adresy e-mail z tekstu zbiorczego za pomocą jquery

Mam ten tekst poniżej:

[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]>

Tutaj e-maile są rozdzielane przez , lub ;. Chcę wyodrębnić wszystkie e-maile obecne powyżej i zapisać je w tablicy. Czy istnieje łatwy sposób za pomocą regex, aby uzyskać wszystkie e-maile bezpośrednio?

Author: Philip Kirkbride, 2013-01-21

6 answers

Oto jak możesz to zrobić:

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]>';    

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'));

Wynik

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

Źródło: Wyodrębnij wiadomość e-mail z tekstu zbiorczego (z wyrażeniami regularnymi, JavaScript i jQuery)

Demo 1 Tutaj

Demo 2 Tutaj używanie każdej funkcji iteratora jQuery

 73
Author: Leniel Maccaferri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-31 17:43:27

Możesz użyć tego wyrażenia regularnego:

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;

Możesz wyodrębnić e-maile w następujący sposób:

('[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]"]
 10
Author: Minko Gechev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-01-21 14:15:12

Tylko uaktualnienie do zaakceptowanej odpowiedzi. Nie działa to w przypadku znaków "plus" na adresie e-mail. GMAIL obsługuje [email protected].

Zaktualizowałem do:

return text.match(/([a-zA-Z0-9._+-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
 8
Author: Nick Caruso,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-01 15:25:58
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)

Źródło

 2
Author: Johan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:03:02

Nie potrzebujesz do tego jQuery; JavaScript sam obsługuje wyrażenia regularne wbudowane.

Spójrz na Wyrażenie regularne Aby uzyskać więcej informacji na temat używania regex z JavaScript.

Poza tym, myślę, że dokładną odpowiedź na swoje pytanie znajdziesz gdzieś indziej na Stack Overflow - Jak znaleźć e-maile i nazwy z ciągu znaków w javascript

 2
Author: Roy Dictus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:32

Powyższa funkcja jest RFC2822 zgodna z 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

 1
Author: Sebastien H.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-29 13:06:24