This question already has answers here:
How can I get query string values in JavaScript?
                                
                                    (73个答案)
                                
                        
                                5年前关闭。
            
                    
目前,我正在这样做:

var token = document.location.href.split('?_sft_category=')[1];


如果我的网址是:

http://www.example.com/xchanges/results/?_sft_category=sky


在这种情况下,我得到sky作为var,但是如果我的URL变为

http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA


在那种情况下,我显然得到了=之后的所有内容,而我只想获得'?_sft_category='之后的第一个字符串

最佳答案

url = "http://www.example.com/xchanges/results/?_sft_category=sky";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));
url ="http://www.example.com/xchanges/results/?_sft_category=sky#comboFilters%5BAgency%5D=.TBWA";
alert(url.split('?_sft_category=')[1].match(/[A-z]+/));


Demo

10-06 08:26