0

I am trying to get query and filter value from this url:

http://xyz/search.page?query=apple&filter=all

using this function:

getParameters: function(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(([^&#]*)=|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(unescape(results[2].replace(/\+/g, " ")));
}

It will return query=apple for this but if we have value like apple# or apple& it will not consider those special character it will still return apple. but for all the rest of special character like !,@,* this will return apple! or apple@ etc. Can someone help me getting proper result for # and & also, Thanks in advance.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    & and # should not appear unescaped inside a query string values, they have special meaning. Consider `x=apple&lemon&cake=y` is it 1, 2 or 3 values/items? – Alex K. Dec 20 '16 at 12:10
  • 1
    There are better ways to parse a URL than to use regex, I suggest you try a different approach – musefan Dec 20 '16 at 12:10
  • 1
    Just for reference, [here is a quick example](https://jsfiddle.net/5he1guj1/) without using regex. Though as Alex K said, your URL needs to be correctly encoded in the first place. You cannot reliably work with a URL that has `&` and `#` in as they are special characters. – musefan Dec 20 '16 at 12:26
  • Your regex escaping is very simplistic. And your matching of query parts is making weird assumptions. Also, what's the `unescape` good for? – Bergi Dec 20 '16 at 12:27
  • [There are so many better methods](http://stackoverflow.com/a/2880929/1048572). Use them. – Bergi Dec 20 '16 at 12:30
  • 1
    `unescape()` is basically the primitive non-Unicode aware version of `decodeURIComponent()`. It doesn't make sense to use both. – Álvaro González Dec 20 '16 at 12:39
  • 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) – Peter B Dec 20 '16 at 12:43
  • @PeterB His function looks like the one in the first answer there. – Barmar Dec 20 '16 at 12:45
  • @musefan Thanks for the answer but in that example if you give this url:http://xyz/search.page?query=apple&grapes&filter=all, it will not return apple&grape rather it will give only apple, is there anything to fix this??? – Nitin yadav Dec 20 '16 at 13:40
  • 1
    @Nitinyadav: That is not a valid URL, if you have that as a URL then you need to fix that problem instead. The first `&` should be escaped for it to be a valid URL – musefan Dec 20 '16 at 13:41

1 Answers1

0

It's always better to use the browser's parser if possible, unless you're running in NodeJS.

Here is a quick implementation of your function:

function getParameters(name, url) {
    var parser = document.createElement('a');
    parser.href = url;
    
    var vars = parser.search.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (decodeURIComponent(pair[0]) == name) {
            return decodeURIComponent(pair[1]);
        }
    }

    return null;
}

console.log(getParameters("filter", "http://xyz/search.page?query=apple&filter=all"));
aurbano
  • 3,324
  • 1
  • 25
  • 39