正则表达式 URL 检查

发布于 2024-12-13 07:48:56 字数 759 浏览 0 评论 0原文

我遇到了一个简单的正则表达式问题。 我想要做的是检查 URL 以查看它是否包含单词 ajax,如果包含则与 URL 匹配。

例如 给定 URL localhost/ajax/image/get 应该匹配并返回 localhost/ajax/image/get

但是 localhost/image/get 不应该匹配。

我有正则表达式的基本知识,但无法让它发挥作用。

有什么建议吗?我确信这是一个非常简单的案例。

我将其用于 mod 重写规则

mod 重写规则是 RewriteRule ^.?\bajax\b.$ ajax.php?url=$1 [PT,L]

请求的 URL http://localhost:93/public/ajax/image/

它重定向到 ajax.php 文件,但不保留 url 值。

我还有另一条通用规则,如下 RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

请求的 URL http:// /localhost:93/public/imgage/get/

当重定向到 index.php 时,它的 url GET 值中有 /public/image/get/

I am stuck with a simple regex problem.
What I want to do is to check a URL to see if it contains the word ajax and if it does then match on the URL.

For instance
given URL localhost/ajax/image/get
should match and return localhost/ajax/image/get

However localhost/image/get
should not match.

I've basic knowledge of regex but havn't been able to get anything working for it.

Any suggestions? I'm sure this is quite a simple case.

I am using this for a mod rewrite Rule

The mod rewrite rule is
RewriteRule ^.?\bajax\b.$ ajax.php?url=$1 [PT,L]

URL requested http://localhost:93/public/ajax/image/

It redirects to the ajax.php file but the url values are not preserved.

I have another rule which is a general one as follows
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

URL requested http://localhost:93/public/imgage/get/

When this redirects to index.php it has /public/image/get/ in the url GET value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

江南月 2024-12-20 07:48:56

简单的正则表达式。您有多个标签,所以我不太确定您的平台是什么。如果是java:

Pattern regex = Pattern.compile("^.*?\\bajax\\b.*$");

一般来说这应该是正则表达式:

   /^.*?\bajax\b.*$/

解释:

"^" +       // Assert position at the beginning of the string
"." +       // Match any single character that is not a line break character
   "*?" +      // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
"\\b" +      // Assert position at a word boundary
"ajax" +    // Match the characters “ajax” literally
"\\b" +      // Assert position at a word boundary
"." +       // Match any single character that is not a line break character
   "*" +       // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"$"         // Assert position at the end of the string (or before the line break at the end of the string, if any)

最后我建议供进一步阅读。

Simple regex. You have multiple tags so I am not quite sure what your platform is. If it is java :

Pattern regex = Pattern.compile("^.*?\\bajax\\b.*$");

In general this should be the regex :

   /^.*?\bajax\b.*$/

Explanation :

"^" +       // Assert position at the beginning of the string
"." +       // Match any single character that is not a line break character
   "*?" +      // Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
"\\b" +      // Assert position at a word boundary
"ajax" +    // Match the characters “ajax” literally
"\\b" +      // Assert position at a word boundary
"." +       // Match any single character that is not a line break character
   "*" +       // Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
"$"         // Assert position at the end of the string (or before the line break at the end of the string, if any)

Finally I would suggest this for further reading.

泪痕残 2024-12-20 07:48:56

我解决了这个问题,我正在寻找的正则表达式是
^(.ajax.)$

我一直在尝试 ^(ajax)$ 但这没有意义。

感谢您的帮助

I solved the issue, the regex i was looking for is
^(.ajax.)$

I had been trying ^(ajax)$ but this doesn't make sense.

Thanks for help

合久必婚 2024-12-20 07:48:56

它可以进行一些改进吗

[a-z/]*ajax[a-z/]*

,所以让我知道是否需要改进。

What about

[a-z/]*ajax[a-z/]*

it could use some refinement, so let me know if it needs to be improved.

天煞孤星 2024-12-20 07:48:56

它应该像这样简单:

Pattern.matches(regex, str)

so

Pattern.matches("ajax", str)

其中 str 是传入的 url 字符串。

It should be as simple as:

Pattern.matches(regex, str)

so

Pattern.matches("ajax", str)

where str is the url string being passed in.

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