Extract keyword from Google search in Javascript

后端 未结 3 943
后悔当初
后悔当初 2020-12-03 15:50

I\'d like to extract from the url of a Google search the keyword of the search (e.g. for the keyword \"car\" : http://www.google.com/webhp?hl=en#sclient=psy&hl=en&si

3条回答
  •  隐瞒了意图╮
    2020-12-03 16:13

    You don't need a regular expression for this. Modified from http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html:

    function getUrlVars(href)
    {
        var vars = [], hash;
        var hashes = href.slice(href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
            hash = hashes[i].split('=');
            vars.push(hash[0]);
            vars[hash[0]] = hash[1];
        }
        return vars;
    }
    

    You can then do this:

    var v = getUrlVars("http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=car&aq");
    var q = v.q;
    

提交回复
热议问题