I have url like this http://127.0.0.1:8000/dashboard/post?page=2&order=title
I want to remove ?page={number}
or &page={number}
query string
I don't have more knowledge about regular expression, is there an opinion to solve this problem?
I have url like this http://127.0.0.1:8000/dashboard/post?page=2&order=title
I want to remove ?page={number}
or &page={number}
query string
I don't have more knowledge about regular expression, is there an opinion to solve this problem?
URLSearchParams
is now the standard API for manipulating a URL query string.
Working Example:
N.B. In the example below a string variable (var windowLocationSearch
) is used as a stand-in for window.location.search
which actually represents the query string at the end of the 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