Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Jquery修改URL查询字符串_Javascript_Jquery_Url - Fatal编程技术网

Javascript 如何使用Jquery修改URL查询字符串

Javascript 如何使用Jquery修改URL查询字符串,javascript,jquery,url,Javascript,Jquery,Url,我知道这个问题被问了很多次,但这些解决方案都不适合我 我需要做的是用其他值替换url查询字符串 我需要替换URL中的?选项卡=。这是我的要求 网址: 我需要做到: http://localhost:3000/private_search/search_all?tab=featured&term=phy&utf8=✓&rating[]=4&_=1487710144036 http://localhost:3000/private_search/search_all

我知道这个问题被问了很多次,但这些解决方案都不适合我

我需要做的是用其他值替换url查询字符串

我需要替换URL中的
?选项卡=
。这是我的要求

网址:

我需要做到:

http://localhost:3000/private_search/search_all?tab=featured&term=phy&utf8=✓&rating[]=4&_=1487710144036

http://localhost:3000/private_search/search_all?tab=all_schools&term=phy&
utf8=✓&rating[]=4&_=1487710144036
decodeURIComponent(this.url)返回:


正则表达式非常强大,非常适合这种情况,但很难学习。这就是如何在您的示例中使用它

$("#featured-url").attr('href',
  $("#featured-url").attr('href').replace(/\?tab=[^&]+/, '?tab=featured')
);

$("#school-url").attr('href',
  $("#school-url").attr('href').replace(/\?tab=[^&]+/, '?tab=all_schools')
);

下面是对正则表达式的解释:

  • \?
    与?字符(转义,因为?有其他含义)
  • tab=
    匹配该字符串
  • [^&]
    匹配除&
  • +
    希望有一个或多个上一个匹配项

如果上述副本中提供的任何解决方案对您不起作用,您的问题包括您如何实施这些解决方案,以及您尝试这些解决方案时哪些不起作用。也可能重复可能重复感谢@Mikemcaughan的宝贵意见。被接受的答案就是我想要的。
http://localhost:3000/private_search/search_all?tab=playlists&term=phy&utf8=✓&
rating[]=4&_=1487710144036


$("#featured-url").attr('href', decodeURIComponent(this.url)); // need to change url here 
 $("#school-url").attr('href', decodeURIComponent(this.url));  // need to change url here 
$("#featured-url").attr('href',
  $("#featured-url").attr('href').replace(/\?tab=[^&]+/, '?tab=featured')
);

$("#school-url").attr('href',
  $("#school-url").attr('href').replace(/\?tab=[^&]+/, '?tab=all_schools')
);