我有这样的网址http://127.0.0.1:8000/dashboard/post?page=2&order=title
我要删除?page={number}
或&page={number}
查询字符串
我对正则表达式没有更多的了解,是否有解决此问题的见解?
答案 0 :(得分:4)
URLSearchParams
现在是用于处理URL查询字符串的标准API。
工作示例:
NB 在下面的示例中,字符串变量(var windowLocationSearch
)被用作window.location.search
的替代者,实际上代表了最后的查询字符串网址。
// Set window.location.search
var windowLocationSearch = 'page=2&order=title';
// Log window.location.search
console.log(windowLocationSearch);
// New URLSearchParams object
var searchParams = new URLSearchParams(windowLocationSearch);
// delete 'page' key from searchParams
searchParams.delete('page');
// Return query string from searchParams
windowLocationSearch = searchParams.toString();
// Log updated window.location.search
console.log(windowLocationSearch);
URLSearchParams
(MDN)-https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams URLSearchParams.delete()
(MDN)-https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/delete