我正在使用以下代码(在此帮助下
post
)要删除包含“all”的查询字符串参数?
它转向:
https://www.foobar.com/page?year=all&language=all&gender=female
进入
https://www.foobar.com/page?gender=female
如何修改代码以使用变量而不是“/all/”?我希望能够轻松切换出要删除的参数。
现有代码(工作但无变量):
let src = "https://www.foobar.com/page?year=all&language=all&gender=female";
let url = new URL(src);
let re = /all/;
let props = [...new URLSearchParams(url.search)]
.filter(([key, prop]) => !re.test(prop));
url = url.origin + url.pathname;
let params = new URLSearchParams();
props.forEach(([key, prop]) => params.set(key, prop));
url += "?" + params.toString();
新代码(不工作)
let parametervariable = "/somefoovariable/";
let src = "https://www.foobar.com/page?year=all&language=all&gender=female";
let url = new URL(src);
let re = parametervariable;
let props = [...new URLSearchParams(url.search)]
.filter(([key, prop]) => !re.test(prop));
url = url.origin + url.pathname;
let params = new URLSearchParams();
props.forEach(([key, prop]) => params.set(key, prop));
url += "?" + params.toString();
variable):**