正则expression式从数据获取电子邮件ID
是新的常规experssion我有以下数据从这我想要得到唯一的电子邮件ID.How是可能的使用正则expresison
commit 01 emailid: Tests <tests@gmail.com> Date: Wed Jun 18 12:55:55 2014 +0530 details commit 02 emailid: user <user@gmail.com> Date: Wed Jun 18 12:55:55 2014 +0530 location commit 03 emailid: Tests <tests@gmail.com> Date: Wed Jun 18 12:55:55 2014 +0530 france24 commit 04 emailid: developer <developer@gmail.com> Date: Wed Jun 18 12:55:55 2014 +0530 seloger
从这个使用常规experssion如何retirve tests@gmail.com,user@gmail.com,developer@gmail.com
有了这个正则expression式:
emailid: [^<]*<([^>]*)
-
emailid:
匹配string文字 -
[^<]*<
匹配任何不是<
字符,然后匹配<
-
([^>]*)
将所有不是>
字符捕获到组1.这是您的emailid。
在正则expression式演示中 ,查看右侧窗格中的组捕获。 这是我们正在寻找的。
获取独特的电子邮件
对于每场比赛,我们检查emailid是否已经在我们唯一的电子邮件ID数组中。 看到这个JS演示的输出。
var uniqueids = []; var string = 'blah emailid: Tests <tests@gmail.com> emailid: user <user@gmail.com> emailid: Tests <tests@gmail.com> emailid: developer <developer@gmail.com>' var regex = /emailid: [^<]*<([^>]*)/g; var thematch = regex.exec(string); while (thematch != null) { // print the emailid, or do whatever you want with it if(uniqueids.indexOf(thematch[1]) <0) { uniqueids.push(thematch[1]); document.write(thematch[1],"<br />"); } thematch = regex.exec(string); }