0

So I currently have this:

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

if(queries !== "") {

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

It's getting all of the $_GET's that are submitted to a search. Well, for added functionality to display only the "active" attributes used in the search, I had to add more $_GET's and that's screwing up the above code because what it's running the split through is like this:

?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

What I need to do is exclude any "&active_#=true"s that are there and the amount of them won't always be the same.

Would I somehow use Regex for this? I'm not very familiar with it so I don't know where to start with the Regex bit.

Does anybody have any ideas or help for this?

Filburt
  • 17,626
  • 12
  • 64
  • 115
stinkysGTI
  • 573
  • 6
  • 21
  • possible duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – brandonscript Feb 06 '14 at 21:45
  • Create an array of `{name: ..., value: ...}` objects and filter out all the ones whose name starts with `active_`? – Felix Kling Feb 06 '14 at 21:46

1 Answers1

1

You could through a .replace() in there before you split the value, to remove the entries . . . something like:

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

That should take care of getting rid of all of those "active" parameters for you.

talemyn
  • 7,822
  • 4
  • 31
  • 52
  • I couldn't get this to work. I even tried a little test: var str = "fbuefvhbfvhu$active_4=truejdnu"; var test = str.replace(/&active_\d+=true/g, ""); alert(test); – stinkysGTI Feb 06 '14 at 22:30
  • Your sample variable is using a `$` instead of a `&`. ;) – talemyn Feb 06 '14 at 22:42
  • I just tacked on your parameter string (from the original post) to my test page and ran the code that I posted and the result was: `["search_type=physical", "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"]`. Worked in FF, Chrome, and IE9 . . . – talemyn Feb 06 '14 at 22:51
  • 1
    HAHA now it works. Well, that was embarrassing lol Thank you :) – stinkysGTI Feb 06 '14 at 22:54