function getUrlArg(url,key){ var arr = url.split('?'); var argStr = arr.length >1 ? arr[1]:""; if(!argStr){ return ""; } var arr2 = argStr.split('&').map(item=>item.split('=')); var res = arr2.find(item=>item[0] == key); if(res){ return res.length > 1 ? res[1] :""; }else{ return ""; } } var url = "https://www.xxx.com/s?name=zhang&age=12&aa"; console.log(getUrlArg(url,"name")); console.log(getUrlArg(url,"age")); console.log(getUrlArg(url,"sex"));