标签:javascript 正则表达式
一:正则表达式法
<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()正则表达式的匹配,具体不会用 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>注:
二: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 }
js分解url参数(正则表达式,split比较)(面向对象-极简主义法应用)
标签:javascript 正则表达式
原文地址:http://blog.csdn.net/hehaoyang666/article/details/41804367