2014-02-06 128 views
0

所以我現在有這樣的:如何排除某些可變部分?

var queries = window.location.search.slice(1).split("&"); 

if(queries !== "") { 

    var count = 0, 
........ 
........ 

它讓所有的被提交給搜索的$ _GET的。那麼,對於新增的功能顯示在搜索中使用只有「活躍」屬性,我不得不添加更多_GET的$,這就是搞砸了上面的代碼,因爲就是它的運行通過分割是這樣的:

?search_type=physical&4=64_to_74_weight_1&active_4=true&10=70_to_84_weight_1&active_10=true&6=0_to_0_weight_1&7=0_to_0_weight_1&8=0_to_0_weight_1&9=0_to_0_weight_1&118=0_to_0_weight_1 

我需要做的是排除任何「& active _#= true」,並且它們的數量不總是相同的。

我會以某種方式使用正則表達式嗎?我對它不是很熟悉,所以我不知道從Regex位開始。

有沒有人有任何想法或幫助?

+0

可能重複[如何在JavaScript中獲取查詢字符串值?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – brandonscript

+0

創建一個'{name:...,value:...}'對象的數組,並過濾​​掉所有以'active_'開頭的名字? –

回答

1

你可以通過.replace()那裏在你分割值之前去除條目。 。 。像這樣:

var queries = window.location.search.slice(1).replace(/&active_\d+=true/g, "").split("&"); 

這應該照顧擺脫所有這些「活躍」的參數給你。

+0

我無法得到這個工作。我甚至嘗試了一下測試:var str =「fbuefvhbfvhu $ active_4 = truejdnu」; var test = str.replace(/&active_ \ d + = true/g,「」); alert(test); – stinkysGTI

+0

你的示例變量使用'$'而不是'&'. ;) – talemyn

+0

我只是將你的參數字符串(從原始文章)加入到我的測試頁,並運行我發佈的代碼,結果是:'[「search_type =物理「,」4 = 64_to_74_weight_1「,」10 = 70_to_84_weight_1「,」6 = 0_to_0_weight_1「,」7 = 0_to_0_weight_1「,」8 = 0_to_0_weight_1「,」9 = 0_to_0_weight_1「,」118 = 0_to_0_weight_1「]。在FF,Chrome和IE9中工作。 。 。 – talemyn