用正則把url解析為對象

來源:互聯網
上載者:User

標籤:

用正則把url解析為對象

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>string and reg</title>
</head>
<body>
<script>
/*
一、知識點
1.\將下一個字元標記為或特殊字元、或原義字元、或反向參考、或八進位轉義符。例如, ‘n‘ 匹配字元 ‘n‘。‘\n‘ 匹配分行符號。序列 ‘\\‘ 匹配 "\",而 ‘\(‘ 則匹配 
"("。
2. ?匹配前面的子運算式零次或一次,或指明一個非貪婪限定符。要匹配 ? 字元,請使用 \?。
3. |替換,"或"操作字元具有高於替換運算子的優先順序,使得"m|food"匹配"m"或"food"。若要匹配"mood"或"food",請使用括弧建立子運算式,從而產生"(m|f)ood"。
4. g 執行全域匹配,而不是找到第一個匹配就停止.
*/
var mylocation = {
‘protocol‘:‘‘,
‘hostname‘:‘‘,
‘pathname‘:‘‘,
‘query‘:‘‘
}
var url = ‘http://adtk.cn/index.html?key1=1&key2=2‘;
var arr=url.split(/\:\/\/|\/|\?/g);
var j=0;
for(i in mylocation){
mylocation[i]=arr[j];
j++;
}
console.log(mylocation);
/*
結果是:
Object {protocol: "http", hostname: "adtk.cn", pathname: "index.html", query: "key1=1&key2=2"}
hostname: "adtk.cn"
pathname: "index.html"
protocol: "http"
query: "key1=1&key2=2"
__proto__: Object
*/
</script>
</body>
</html>

 分類: Regex (r

用正則把url解析為對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.