您可以使用正则表达式来提取查询字符串参数。请参考以下问题。
如何在 JavaScript 中获取查询字符串值?
小提琴样本
根据此处的上下文编辑的答案中的相关代码:
function getParameterByName(name,url) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var one = '/news/local/edit?user=marcus&owner=ffff';
var two = '/news/other/edit?user=josh&owner=ddd';
var three = '/news/local/edit?user=john';
var four = '/news/local/test/marcus/edit?user=ady&owner=aaaa';
alert(getParameterByName("user",one));
alert(getParameterByName("user",two));
alert(getParameterByName("user",three));
alert(getParameterByName("user",four));