解析JavaScript中的URL

如何使用JavaScript(以及jQuery)解析URL?


例如,我的字符串中有这个


url = "http://example.com/form_image_edit.php?img_id=33"

我想得到的价值 img_id


我知道我可以使用PHP轻松地做到这一点parse_url(),但是我想知道JavaScript怎么可能。


UYOU
浏览 547回答 3
3回答

侃侃无极

如果你的字符串被称为s然后var id = s.match(/img_id=([^&]+)/)[1]会给你的。

POPMUISE

是从Google获得的,请尝试使用此方法function getQuerystring2(key, default_)&nbsp;{&nbsp;&nbsp; &nbsp; if (default_==null)&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; default_="";&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; var search = unescape(location.search);&nbsp;&nbsp; &nbsp; if (search == "")&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return default_;&nbsp;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; search = search.substr(1);&nbsp;&nbsp; &nbsp; var params = search.split("&");&nbsp;&nbsp; &nbsp; for (var i = 0; i < params.length; i++)&nbsp;&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var pairs = params[i].split("=");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(pairs[0] == key)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return pairs[1];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;return default_;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP