用正则表达式和KRL的replace方法解析url

发布于 2024-10-04 19:57:03 字数 1818 浏览 0 评论 0原文

我想获取当前页面的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˉ厌 2024-10-11 19:57:03

2种方法来实现你想要的。

1) 在预块

pre {
  queryInURL = page:url("query");
  q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
}
  • 页面中:url("query") 抓取 url 中的整个参数字符串,
  • 执行字符串替换以捕获您想要

在 url 上测试的完整示例应用程序测试的

特定查询参数-> http://example.com/?q=cats&wow=cool

< img src="https://i.sstatic.net/CPFLq.png" alt="alt text">

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 显示的方式

2 Ways to accomplish what you want.

1) In the pre block

pre {
  queryInURL = page:url("query");
  q = queryInURL.replace(re/.*?q=(.*?)(?:$|&.*)/,"$1");
}
  • page:url("query") grabs the entire string of parameters in a url
  • do string replace to capture specific query parameter that you want

Full Example App Tested

Tested on url -> http://example.com/?q=cats&wow=cool

alt text

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) In the rules selection expression the way JAM has shown

不必在意 2024-10-11 19:57:03

这可以在 select 语句中使用正则表达式和捕获组 (()'s) 来完成。

select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)" setting(query)

正则表达式使 select 语句变得强大。一定要学习它们! 这里是一个优秀的正则表达式(或正则表达式)网站。

This can be done in the select statement using a regular expression and a capturing group (()'s).

select when pageview "http://www.google.com/search.*(?:&|?)q=(\w+)(?:&|$)" setting(query)

Regular expressions make the select statement powerful. Be sure to learn them! Here is an excellent Regular Expression (or Regex) site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文