通过删除一个参数及其值来清理URL

时间:2015-02-04 12:30:57

标签: javascript jquery

我们说我有以下网址:

www.mydomain.com/default.aspx?param1=value1&param2=value2&param3=value3
OR
www.mydomain.com/default.aspx?param2=value2&param1=value1
OR
www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2

如何只删除部分" param1 = value1"从那些使用jQuery或Javascript的URL地址?

2 个答案:

答案 0 :(得分:2)

试试这个: -

function RemoveParam(url, p) {
   return url
  .replace(new RegExp('[?&]' + p + '=[^&#]*(#.*)?$'), '$1')
  .replace(new RegExp('([?&])' + p + '=[^&]*&'), '$1');
}

var url='www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2'

 alert(RemoveParam(url,'param1'));

Demo

答案 1 :(得分:1)

var url = "www.mydomain.com/default.aspx?param3=value3&param1=value1&param2=value2" 
url = url.replace("&param1=value1", "");