使用正则表达式和 Jquery 删除 Url 和包含 Id
Removing Url and Include Id using Regex and Jquery
我是 Regex 的新手,我的问题是如何从 url 中包含 ID 并使用 Regex 删除它?因为截至目前,它只删除了 actionMe=reload&Id=
但 ID 仍然是 return 所以在删除它并替换为新的 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, '' + key + "=" + value + '');
}
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;
很确定您只需要 /&Id=\d+/
作为您的 RegExp。不需要 select actionMe=reload
中的任何一个,除非您需要它来规范(在这种情况下,只需将其添加回去)。您的其余代码按预期工作,只是您的正则表达式没有 select 您想要的精确部分。
解释:
正则表达式的 (\&|&)
部分是多余的,因为 &
不需要转义即可工作。事实上,由于它在括号中,您最终会捕获 &
字符,如果您真的需要该部分,请尝试 (?:\&|&) 忽略捕获组。您的代码替换了匹配的正则表达式,但在 Id= 之后没有包含数字“15”,这就是为什么它在您编辑的版本之后附加了 15,因为它不匹配,因此没有被替换。添加 \d+ 将 select 任何尾随数字。这应该会给你想要的结果。
我是 Regex 的新手,我的问题是如何从 url 中包含 ID 并使用 Regex 删除它?因为截至目前,它只删除了 actionMe=reload&Id=
但 ID 仍然是 return 所以在删除它并替换为新的 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, '' + key + "=" + value + '');
}
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;
很确定您只需要 /&Id=\d+/
作为您的 RegExp。不需要 select actionMe=reload
中的任何一个,除非您需要它来规范(在这种情况下,只需将其添加回去)。您的其余代码按预期工作,只是您的正则表达式没有 select 您想要的精确部分。
解释:
正则表达式的 (\&|&)
部分是多余的,因为 &
不需要转义即可工作。事实上,由于它在括号中,您最终会捕获 &
字符,如果您真的需要该部分,请尝试 (?:\&|&) 忽略捕获组。您的代码替换了匹配的正则表达式,但在 Id= 之后没有包含数字“15”,这就是为什么它在您编辑的版本之后附加了 15,因为它不匹配,因此没有被替换。添加 \d+ 将 select 任何尾随数字。这应该会给你想要的结果。