javascript - String.Match()无法按预期工作

时间:2012-01-23 02:04:24

标签: javascript regex string

使用以下字符串:

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&tab=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 { {3}} http://books.google.com.ar/bkshp?hl=es&tab=wp http://www.google.com.ar/intl/es/options/ http://www.google.com.ar/blogsearch?hl=es&tab= wb http://scholar.google.com.ar/schhp?hl=es&tab=ws https://docs.google.com/?tab=wo https://www.google.com/calendar?tab=wc http://groups.google.com.ar/grphp?hl=es&tab=wg {{3} http://www.google.com.ar/intl/es/options/ https://sites.google.com/?tab=w3 http://www.google.com.ar/preferences?hl=es http://www.google.com.ar/reader/?hl=es&tab=wy http:// www.google.com.ar/advanced_search?hl=es-419 https://accounts.google.com/ServiceLogin?hl=es&continue=http://www.google.com.ar/ http://www.google.com/history/optout?hl=es http://www.google.com.ar/preferences?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/language_tools?hl=es-419 http://www.google.com.ar/services/ {{3} } https://plus.google.com/112209395112018703654 http://www.google.com.ar/webhp?hl=es-419 http://www.google.com/ncr javascript:void(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/intl/es-419/ads/
  2. 的http://
  3. 万维网。
  4. google.com.ar
  5. 我已经尝试在http://www.google.com.ar/intl/es-419/privacy.html和www.regextester.com中测试正则表达式,在这两种情况下突出显示的匹配都是正确的,所以我想问题是代码。 我是javascript的新手,所以我看不出问题。

    提前致谢

1 个答案:

答案 0 :(得分:1)

使用正则表达式上的g标记。

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