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"]).
我想从谷歌搜索的url中提取搜索的关键词(例如:关键词“car”:http://www.google.com/webhp?hl = en # sclient = psy&hl = en&site = webhp&source = hp&q = car&aq……(这里的car在“q=”和“&aq”之间,但是我注意到代币可能会改变“&ie”而不是“&aq”)。
Being new at regex and Google search I haven't found so far the solution, does someone know how to do this please ?
作为regex和谷歌搜索的新手,到目前为止我还没有找到解决方案,请问有人知道怎么做吗?
Bruno
布鲁诺
3 个解决方案
#1
18
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:
这个不需要正则表达式。从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;
#2
0
Below sample code works for me for a single parameter and current URL but can be modified as per requirement.
下面的示例代码为我提供了一个参数和当前URL,但可以根据需要修改。
function extract( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
just use it as shown below,
如下图所示,
var result= extract("your parameter name");
#3
0
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'));
#1
18
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:
这个不需要正则表达式。从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;
#2
0
Below sample code works for me for a single parameter and current URL but can be modified as per requirement.
下面的示例代码为我提供了一个参数和当前URL,但可以根据需要修改。
function extract( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
just use it as shown below,
如下图所示,
var result= extract("your parameter name");
#3
0
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'));