2012-01-23 27 views
0

使用下面的字符串不工作:的JavaScript String.Match()如預期

http://www.google.com.ar/setprefs?prev=http://www.google.com.ar/&sig=0_Kxz_cp1G52p8pcrDBlMIQhwJAL0%3D&suggon=2 https://plus.google.com/?gpsrc=ogpy0 &標籤= WX http://www.google.com.ar/webhp?hl=es&tab=ww http://www.google .com.ar/imghp?hl = es & tab = wi http://video.google.com.ar/?hl=es&tab=wv http://news.google.com.ar/nwshp?hl=es & tab = wn http://translate.google.com.ar/?hl=es&tab=wT https://mail.google.com/mail/? tab = wm http://www.google.com.ar/intl/es/options/ http://books.google.com.ar/bkshp?hl=es & tab = wp http://scholar.google.com.ar/schhp?hl=es&tab=ws http://www.google.com.ar/blogsearch?hl=es & tab = wb https://www.google.com/calendar?tab=wc https: //docs.google.com/?tab=wo https://sites.google.com/?tab=w3 http:// gro ups.google.com.ar/grphp?hl=es & tab = wg http://www.google.com.ar/reader/?hl=es&tab=wy http://www.google.com.ar/intl/es/options/ https://accounts.google.com/ServiceLogin?hl=es&continue=http://www.google.com.ar/ http://www.google.com.ar/preferences ?hl = es http://www.google.com.ar/preferences?hl=es-419 http://www.google.com.ar/advanced_search?hl=es-419 http://www.google.com.ar/language_tools?hl=es-419 http://www.google.com/history/optout?hl=es http://www.google.com.ar/webhp?hl=es-419 http://www.google .com.ar/support/websearch/bin/answer.py?answer = 186645 & form = bb & hl = es-419 http://www.google.com.ar/intl/es-419/ads/ http://www.google.com.ar/services/ http://www.google.com.ar/intl/es-419/privacy.html https:// plus。 google.com/112209395112018703654 http://www.google.com.ar/intl/es-419/about.html http://www.google.com/ncr的javascript:無效(0)

而且此正則表達式:

(HTTP://)(WWW){0,1}(google.com.ar)[\ S] *

此代碼:

var result = links.match(new RegExp("(http://)(www.){0,1}(google.com.ar)[\S]*")); 
for(var i = 0;i<result.length;i++) 
{ 
    alert(result[i]); 
} 

使我這個輸出:

  1. http://www.google.com.ar
  2. 的http://
  3. WWW。
  4. google.com.ar

我已經試圖測試正則表達式中http://regexpal.com/和www.regextester.com,並在這兩種情況下高亮匹配是正確的,所以我想這個問題是與代碼。 我真的很新的JavaScript,所以我不能看到問題。

在此先感謝

+1

......你在期待什麼沒有發生? – Amber

+3

「......沒有按預期工作」你期望的**是什麼**? –

+0

我預計正確的匹配,例如正則表達式應至少匹配:http://www.google.com.ar/intl/es-419/about.html 但它不這樣做。 – BizTuk

回答

1

使用g標誌在你的正則表達式。

var result = links.match(/http:\/\/(?:www\.)?google\.com\.ar([\S]*)/g); 
+0

謝謝,這工作完美。但我需要的網頁是一個變量,有沒有什麼辦法來存檔這與我發佈的正則表達式「格式」? – BizTuk

+0

好吧,如果循環遍歷'result'數組,那麼可以再次測試正則表達式,但不使用'g'標誌。這將允許您獲取子模式。 –

+0

這就是我的意思:new RegExp(「(http://)(www。){0,1}(」+ URL +「)[\ S] *」)。我需要一種更簡單的方法來獲取來自任何**網站的鏈接(不包括子域名http://groups.google.com.ar)。 – BizTuk