【发布时间】:2012-11-30 22:31:27
【问题描述】:
我有一个与 example.com/?id[]=15029&id[]=18711&foo=bar#MyTab 类似的网址
我想获取url中的所有id
var str = window.location.search;
var result = {};
str.replace(/([^?=&]+)(?:[&#]|=([^&#]*))/g, function (match, key, value) {
result[key] = value || 1;
});
result;
以上代码只返回最后一个id参数。
如何改进它以获取数组中的所有 id?
【问题讨论】:
-
为什么是正则表达式?
split('&').forEach,完成。 -
看看我的正则表达式如何处理
#的 -
看看this的帖子。
-
@Mithun
location.search根本不包括location.hash。 -
@Mike Corcoran 那里没有答案处理
[]case
标签: javascript regex url