Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract keyword from Google search in Javascript

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&site=webhp&source=hp&q=car&aq ... (here car is between "q=" and "&aq" but I noticed that the tokens might change ["&ie" instead of "&aq"]).

Being new at regex and Google search I haven't found so far the solution, does someone know how to do this please ?

Bruno

like image 972
Bruno Avatar asked May 18 '11 13:05

Bruno


1 Answers

function getParameterByName(name,url) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(url);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


alert(getParameterByName('q','https://www.google.com/search?q=buy+a+pc&oq=buy+a+pc&aqs=chrome..69i57j5j0l2j69i61.1674j0&sourceid=chrome&ie=UTF-8#psj=1&q=buy+a+pc'));
like image 191
Shemeer M Ali Avatar answered Sep 28 '22 02:09

Shemeer M Ali