我想获取当前页面的URL(使用page:env(“caller”))并提取其中的一部分。
例如,我想采取
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats
并指定
cats
变量。
我如何使用KRL进行此操作?
我试过了
url = page:env("caller");
query = url.replace("http://www\.google\.com/search\?sourceid=chrome&ie=UTF-8&q=", "");
但它只是将整个页面:env(“调用者”)分配给变量查询(例如http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats)。
编辑:jQuery解决方案也很有可能。
Edit2:@JAM -
您发布的select语句似乎不起作用。我在http://www.google.com/search?q=cats进行了测试,并没有开火。不确定网址是否与网页浏览不匹配(看起来它应该与我匹配)。
我把它放入的应用程序:
ruleset a835x36 {
meta {
name "regex testing2"
description <<
>>
author ""
logging on
}
rule get_query {
select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)" setting(query)
notify("Query",query) with sticky = true;
}
}
此外,我正在寻找一种更强大的方式来获取查询,因为Google有很多方法可以登陆搜索结果页面,其网址看起来不像http://www.google.com/search?q=cats。例如,去谷歌和搜索猫只给了http://www.google.com/webhp?hl=en#sclient=psy&hl=en&site=webhp&source=hp&q=cats&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=8ac6b4cea9b27ecb的结果的URL。我想我可以用正则表达式解析任何东西,但是......
答案 0 :(得分:5)
2种方法可以达到你想要的效果。
1)在前一栏中
pre {
queryInURL = page:url("query");
q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
}
完整示例应用程序测试
在网址上测试 - &gt; http://example.com/?q=cats&wow=cool
ruleset a60x439 {
meta {
name "url query test"
description <<
Getting the query from the current page URL
>>
author "Mike Grace"
logging on
}
rule get_query {
select when pageview ".*"
pre {
queryInURL = page:url("query");
q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
}
{
notify("Query",queryInURL) with sticky = true;
notify("q",q) with sticky = true;
}
}
}
2)在规则选择表达式中,JAM显示的方式
答案 1 :(得分:4)
这可以使用正则表达式和捕获组(()的)在select语句中完成。
select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)" setting(query)
正则表达式使select语句功能强大。一定要学习它们! Here是一个优秀的正则表达式(或正则表达式)网站。