如何清除url中指定参数,并返回url?

export function deleteUrlQueryByName(param) {
  const reg = new RegExp('(^|&)' + param + '=([^&]*)(&|$)');
  const r =
    window.location.search.substr(1).match(reg) ||
    window.location.hash
      .substring(window.location.hash.search(/\?/) + 1)
      .match(reg);
  if (r != null) {
    return window.location.href.replace(
      `${param}=${decodeURIComponent(r[2])}`,
      '',
    );
  }
  return window.location.href;
}

你可能感兴趣的:(javascript前端)