2010-04-20 62 views

回答

1
function getSubdomain() { 
    var re = /http\:\/\/(\w+)\.somesite\.com\//; 
    return (re(document.location)[1]); 
} 
+0

我很困惑,爲什麼這個答案.....它有一個甚至沒有使用過的參數'domainString' ... – Reigel 2010-04-20 06:31:52

+0

Reigel,oops。固定。 – sblom 2010-04-20 06:40:03

+0

那麼,正則表達式的工作原理(當我剛開始理解正則表達式概念時,我無法弄清楚)。但是,有一個問題,那裏的document.location在做什麼? – Warrantica 2010-04-20 06:42:41

0

如果你問一個正則表達式這則以下(在Perl)應該這樣做,假設$海峽包含http://somesubdomain.somesite.com/

$str =~ s/http:\/\/([^.]+)\.somesite\.com\//$1/ 
print $str; 

這將打印somesubdomain。

解釋 - 正則表達式從字符串http://somesubdomain.somesite.com/中捕獲somesubdomain到第一個返回引用中,然後用它替換整個字符串。