const deleteParams = (deleteStr) => {
const queryReplace = (query, str) => {
// str + "\=[^\&]+?\& 对应 str=xxxx&
// [\&|\?]" + str + "\=[^\&]+$ 对应 &str=xxx结束 或者 ?str=xxx结束
var reg = new RegExp(str + "\=[^\&]+?\&|[\&|\?]" + str + "\=[^\&]+$")
return query.replace(reg, '')
}
const locationSearch = queryReplace(location.search, deleteStr)
history.replaceState({}, '', location.origin + location.pathname + locationSearch)
console.log(`%c locationSearch的值是:%o`, 'background-color:#0f0;color:#f00;font-size:20px;', locationSearch)
}
比如当前页面链接是https://www.baidu.com/?aaa=123&bbb=456&ccc=777
deleteParams('aaa') ===>https://www.baidu.com/?bbb=456&ccc=777
deleteParams('bbb') ===>https://www.baidu.com/?aaa=123&ccc=777
deleteParams('ccc') ===>https://www.baidu.com/?aaa=123&bbb=456
网友评论