Javascript 如何使用Jquery修改URL查询字符串
我知道这个问题被问了很多次,但这些解决方案都不适合我 我需要做的是用其他值替换url查询字符串 我需要替换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
?选项卡=
。这是我的要求
网址:
我需要做到:
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=
匹配除&[^&]
希望有一个或多个上一个匹配项+
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')
);