js分解url參數(Regex,split比較)(物件導向-極簡主義法應用)

來源:互聯網
上載者:User

js分解url參數(Regex,split比較)(物件導向-極簡主義法應用)

一:Regex法

<script type="text/javascript">function getQueryString(url) {    if(url) {        url=url.substr(url.indexOf("?")+1); //字串截取,比我之前的split()方法效率高    }    var result = {}, //建立一個對象,用於存name,和value    queryString =url || location.search.substring(1), //location.search設定或返回從問號 (?) 開始的 URL(查詢部分)。    re = /([^&=]+)=([^&]*)/g, //正則,具體不會用    m;    while (m = re.exec(queryString)) { //exec()Regex的匹配,具體不會用        result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]); //使用 decodeURIComponent() 對編碼後的 URI 進行解碼    }    return result;}// demo    var myParam = getQueryString("www.taobao.com?key0=a&key1=b&key2=c");    alert(myParam.key1);</script> 
註:
1、substr()與substring(start,stop) ,提取字串中介於兩個指定下標之間的字元。
重要事項:與 slice() 和 substr() 方法不同的是,substring() 不接受負的參數。
參見http://www.jb51.net/w3school/js/jsref_substring.htm
2、location.search.substring(1) ,location.search設定或返回從問號 (?) 開始的 URL(查詢部分)。
參見http://www.jb51.net/w3school/htmldom/prop_loc_search.htm
3、exec() 方法用於檢索字串中的Regex的匹配。太強大了,還不會用
參考http://www.jb51.net/w3school/js/jsref_exec_regexp.htm
4、使用 decodeURIComponent() 對編碼後的 URI 進行解碼。
參見http://www.jb51.net/w3school/js/jsref_exec_regexp.htm

二:split法(JAVASCRIPT GUIDE 上COPY的.)

/*  * This function parses ampersand-separated name=value argument pairs from  * the query string of the URL. It stores the name=value pairs in  * properties of an object and returns that object. Use it like this:  *  * var args = getArgs( );  // Parse args from URL  * var q = args.q || "";  // Use argument, if defined, or a default value  * var n = args.n ? parseInt(args.n) : 10;  */  function getArgs( ) {      var args = new Object( );      var query = location.search.substring(1);     // Get query string      var pairs = query.split("&");                 // Break at ampersand      for(var i = 0; i < pairs.length; i++) {          var pos = pairs[i].indexOf('=');          // Look for "name=value"          if (pos == -1) continue;                  // If not found, skip          var argname = pairs[i].substring(0,pos);  // Extract the name          var value = pairs[i].substring(pos+1);    // Extract the value          value = decodeURIComponent(value);        // Decode it, if needed          args[argname] = value;                    // Store as a property      }      return args;                                  // Return the object  }  



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.