用正则表达式和KRL的replace方法解析url
我想获取当前页面的 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=", "");
,但它只是将整个 page:env("caller") 分配给变量查询(例如 http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats)。
编辑:jQuery 解决方案很可能也可以工作。
Edit2:@JAM——
您发布的 select 语句似乎不起作用。我在 http://www.google.com/search?q=cats 上进行了测试但它没有着火。不确定该 URL 是否与浏览量不匹配或什么(看起来应该与我匹配)。
我放入的应用程序:
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。例如,访问 google 并搜索 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。我想我可以用正则表达式解析任何东西,但......
I want to take the current page's URL (using page:env("caller")) and extract a section of it.
For instance, I want to take
http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats
and assign
cats
to a variable.
How would I do this with KRL?
I have tried
url = page:env("caller");
query = url.replace("http://www\.google\.com/search\?sourceid=chrome&ie=UTF-8&q=", "");
but it simply assigns the entire page:env("caller") to the variable query (e.g. http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=cats).
Edit: a jQuery solution would most likely work, as well.
Edit2: @JAM --
The select statement you posted doesn't seem to work. I tested it on http://www.google.com/search?q=cats and it didn't fire. Not sure if the URL doesn't match pageview or what (it looks like it should match to me).
The app I put it in:
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;
}
}
Also, I'm looking for a more robust way to get at the query, since Google has many ways to land on a search results page with URLs that won't look like http://www.google.com/search?q=cats. For example, going to google and searching for cats just gave 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 for the URL of the results. I guess I could parse anything with a regex, though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2种方法来实现你想要的。
1) 在预块
在 url 上测试的完整示例应用程序测试的
特定查询参数-> http://example.com/?q=cats&wow=cool
< img src="https://i.sstatic.net/CPFLq.png" alt="alt text">
2) 在规则选择表达式中 JAM 显示的方式
2 Ways to accomplish what you want.
1) In the pre block
Full Example App Tested
Tested on url -> http://example.com/?q=cats&wow=cool
2) In the rules selection expression the way JAM has shown
这可以在 select 语句中使用正则表达式和捕获组 (()'s) 来完成。
正则表达式使 select 语句变得强大。一定要学习它们! 这里是一个优秀的正则表达式(或正则表达式)网站。
This can be done in the select statement using a regular expression and a capturing group (()'s).
Regular expressions make the select statement powerful. Be sure to learn them! Here is an excellent Regular Expression (or Regex) site.