<body> <script type="text/javascript"> var url = "http://192.168.1.82:8020/juzhong/getURLvalue.html?name1=zhangyiggg&sex1=nuuu&age1=8090"; var name1 = getUrl(url,"name1"); var sex1 = getUrl(url,"sex1"); var age1 = getUrl(url,"age1"); console.log("name1= "+name1); console.log("sex1= "+sex1); console.log("age1= "+age1); //链接带后缀!用 function getUrl(url,name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = url.split("?")[1].match(reg); // var r = url.split(name+"=")[1].split("!")[0]; if (r != null) return decodeURI(r[2]); return null; } </script> </body>
输出为:
思路:供学习!!
/* https://www.baidu.com/s?name=avery&age=20&sex=male&like=talkwithothers 取出name,age和sex的值 */ var str = "https://www.baidu.com/s?name=avery&age=20&sex=male&like=talkwithothers"; var st1 = str.split("?"); var arr = st1[1]; var st2 = arr.split("&"); for (var i=0;i<=st2.length-1;i++) { var ss = st2[i].split("="); var dd = ss[0]; var ff = ss[1]; if (ff!="") { document.write(" "+dd+":"+ff+"<br />"); } }