我需要从 URL1 获取查询字符串,然后重定向到 URL2 - 附加来自 URL1 的查询字符串。
查询字符串有 2 个值:
http://URL1.com/hi/j?a=123&b=666
我希望它重定向到:
http://URL2.com/hi/b?a=123&b=666
我在 Google 上找不到任何有关执行此操作的信息。我发现了这个,但它只适用于 1 个值,并且似乎受到了批评 - 而且它只接受查询字符串而不添加它:How can I get query string values in JavaScript?
谢谢。
最佳答案
尝试这个模式 \?(.*)
。匹配 ?
之后的正则表达式模式。然后粘贴新创建的网址扩展
像这样添加代码
var a =window.location.href;
var after =/\?(.*)/g.exec(a)[0]
window.location.href='http://URL2.com/hi/b'+after
工作示例
var a ='http://URL1.com/hi/j?a=123&b=666';
var after =/\?(.*)/g.exec(a);
console.log(after[1].split('&'))//try with your wish from array(selectquery)
console.log('http://URL2.com/hi/b'+after[0])//for whole query
关于javascript - 使用 javascript 从 URL 获取查询字符串并作为查询字符串传递到新 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449371/