簡體   English   中英

如何使用正則表達式選擇單詞的一部分?

[英]How to select part of the word using regular expression?

我有一些文本,我想在其中找到所有電子郵件部分。 然后只返回domeins部分,它應該類似於:
meth@buisness.com > buisness.com
yaourt@yoplait.fr > oplait.fr
我試圖這樣做:

 var text = "I get a mail from meth@buisness.com (by the way I thought its domain was buisness.com), it was sent to yaourt@yoplait.fr, but the guys of gmail.com said we could only trust the yaourt@cesi.fr adress"; function getDomainsOfEmails(text) { var regMatch = /@([az]+.[az]+)/g; var regReturn = text.match(regMatch); console.log( regReturn); return regReturn; } getDomainsOfEmails(text); 

但是我不知道如何從搜索中排除"@"符號。

RegExp中的括號創建了cature組。 regexp.exec函數返回一個數組,其中0是整個匹配項,然后所有其他單元格從左到右包含捕獲組。 所以這:

var reg = /@([a-z0-9-]+\.[a-z]+)/ig;
reg.exec(text);

首次調用時將返回以下內容:

[
  0: "@yoplait.fr"
  1: "yoplait.fr"
]

如果再次調用它,由於表達式具有全局標志,它將得到下一個結果:

[
  0: "@buisness.com"
  1: "buisness.com"
]

但最好的方法是將其分為兩部分,然后得到第二部分。 試試這個,我希望這是工作

(?<=@)[^.]+(?=\.).*$

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM