2013-01-21 27 views
24

Mam this tekst poniżej:wyodrębnić wszystkie adresy e-mail z tekstem luzem jQuery

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

Tu-maile są seprated przez , lub ;. Chcę wyodrębnić wszystkie wiadomości e-mail obecne powyżej i zapisać je w tablicy. Czy istnieje prosty sposób użycia wyrażenia regularnego, aby uzyskać wszystkie e-maile bezpośrednio?

+0

um, ponieważ próbka "tutaj e-maile są seprated przez' ' 'lub'; '" wydaje się być nieprawidłowy rachunek. czy miałeś na myśli ',' i/lub ';'? –

+0

@MarkSchultheiss: sprawdź listę adresów Gmaila, cc i bcc. Jest ona całkowicie poprawna, aby rozdzielać tam e-maile przecinkami lub średnikami. –

+0

Tak, ale mówisz o pojedynczym cudzysłowie/apostophe, który nie pasuje do Twoich przykładowych danych. –

Odpowiedz

62

Oto w jaki sposób można podejść do tego:

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._-][email protected][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: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)

Demo 1 Here

Demo 2 Here pomocą każdego iterator jQuery

+0

@thanks leniel .... oczekiwałem na iterator jquery dla wszystkich e-maili.i demo 2 działa gr8 fr me –

+0

Naprawdę miło jest wiedzieć, że było pomocne! :) –

+0

Nie powinien przeczytać "extractEmails ($ (" # e-maile "). Text()). Join ('\ n');' lub czy czegoś brakuje? – notacouch

7

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

var re = /(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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żna wyodrębnić e-maile tak:

('[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]"] 
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

+0

thnks johan ... działa świetnie –

4

właśnie aktualizacja z przyjętym odpowiedź. Nie działa to w przypadku znaków "plus" na adresie e-mail. GMAIL obsługuje [email protected]

Mam zaktualizowana:

return text.match(/([a-zA-Z0-9._+-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);