2013-01-21 36 views
24

Tôi đang gặp các văn bản dưới đây:Extract tất cả các địa chỉ email từ văn bản số lượng lớn bằng 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]> 

Đây email được seprated bởi , hoặc ;. Tôi muốn trích xuất tất cả các email hiện tại ở trên và lưu trữ chúng trong mảng. Có cách nào dễ dàng bằng cách sử dụng regex để có được tất cả các email trực tiếp?

+0

um, được đưa ra mẫu của bạn "ở đây email được seprated bởi' ' 'hoặc'; '" dường như là một tuyên bố không hợp lệ. bạn có lẽ có nghĩa là ',' và/hoặc ';'? –

+0

@MarkSchultheiss: xem gmail to, cc và bcc list.its hoàn toàn hợp lệ để seprate email trên đó bằng dấu phẩy hoặc dấu chấm phẩy. –

+0

Có, nhưng bạn nói một trích dẫn/apostophe không khớp với dữ liệu mẫu của bạn. –

Trả lời

62

Đây là cách bạn có thể tiếp cận này:

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

quả

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

Nguồn: Extract email from bulk text (with Regular Expressions, JavaScript & jQuery)

Demo 1 Here

Demo 2 Here sử dụng mỗi chức năng lặp jQuery

+0

@thanks leniel .... tôi đã mong chờ trình duyệt jquery cho tất cả các email.và demo 2 hoạt động gr8 fr me –

+0

Thật sự rất hay khi biết nó hữu ích! :) –

+0

Không nên đọc 'extractEmails ($ (" # email "). Text()). Join ('\ n');' hoặc tôi thiếu cái gì? – notacouch

7

Bạn có thể sử dụng regex này:

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; 

Bạn có thể trích xuất các e-mail như thế này:

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

Bạn không cần jQuery cho rằng; Bản thân JavaScript hỗ trợ regex được tích hợp sẵn.

Hãy xem Regular Expression để biết thêm thông tin về cách sử dụng regex với JavaScript.

Khác hơn thế, tôi nghĩ rằng bạn sẽ tìm ra câu trả lời chính xác cho câu hỏi của bạn ở một nơi khác trên Stack Overflow - How to find out emails and names out of a string in javascript

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 ... hoạt động tuyệt vời –

4

Chỉ cần một cập nhật cho câu trả lời chấp nhận. Điều này không hoạt động đối với các dấu "cộng" trong địa chỉ email. GMAIL hỗ trợ [email protected]

tôi đã cập nhật lên:

return text.match(/([a-zA-Z0-9._+-][email protected][a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi); 
Các vấn đề liên quan