gpt4 book ai didi

javascript - 使用 Regex 和 Jquery 删除 Url 和包含 Id

转载 作者:行者123 更新时间:2023-11-30 12:18:40 25 4
gpt4 key购买 nike

我是 Regex 的新手,我的问题是如何从 url 中包含 ID 并使用 Regex 将其删除?因为截至目前,它只删除了 actionMe=reload&Id= 但 Id 仍然返回,所以在删除它并用新的 Url 替换后,旧的 id 仍然包含在新的 ID 中,

示例,在删除和替换 Url 之前:

http://localhost:2216/Main/WorkerPage?workerId=10&actionMe=reload&Id=15

删除并替换 url 后,它是这样的:

http://localhost:2216/Main/WorkerPage?workerId=10&actionMe=reload&Id=1615

这是我的代码片段:

var sss = $("#Id").val();
if (window.location.href.indexOf("&actionMe=reload&Id=") > -1) {
var regex = /(\&|&)actionMe=reload&Id=/;
var location = window.location.href;
if (regex.test(location)) {
window.location = location.replace(regex, "&actionMe=reload&Id=" + sss)
}

}

谢谢大家的回答:)

最佳答案

您可以使用此代码来更新 url 参数

function updateQueryStringParameter(uri, key, value) {
var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
if (uri.match(re)) {
return uri.replace(re, '$1' + key + "=" + value + '$2');
}
else {
return uri + separator + key + "=" + value;
}
}

我知道了here

现在,在你的情况下,你可以像这样使用它

var url = window.location.href;
var sss = $("#Id").val();
var newUrl = updateQueryStringParameter(url, "id", sss);
//do whatever you want to newUrl
//to redirect to new url
window.location = newUrl;

关于javascript - 使用 Regex 和 Jquery 删除 Url 和包含 Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31690324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com