我有这样的网址http://127.0.0.1:8000/dashboard/post?page=2&order=title
我想删除 ?page={number}
或 &page={number}
查询字符串
我对正则表达式没有更多的了解,有解决这个问题的意见吗?
最佳答案
URLSearchParams
现在是用于操作 URL 查询字符串的标准 API。
工作示例:
注意 在下面的示例中,字符串变量 (var windowLocationSearch
) 用作 window.location.search
的替代变量code> 实际上代表 URL 末尾的查询字符串。
// 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/URLSearchParamsURLSearchParams.delete()
(MDN) - https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/delete- 使用 JavaScript 获取查询字符串参数作者:David Walsh - https://davidwalsh.name/query-string-javascript
关于javascript - 替换或删除查询字符串 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51725167/