正则表达式 URL 检查
我遇到了一个简单的正则表达式问题。 我想要做的是检查 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单的正则表达式。您有多个标签,所以我不太确定您的平台是什么。如果是java:
一般来说这应该是正则表达式:
解释:
最后我建议这供进一步阅读。
Simple regex. You have multiple tags so I am not quite sure what your platform is. If it is java :
In general this should be the regex :
Explanation :
Finally I would suggest this for further reading.
我解决了这个问题,我正在寻找的正则表达式是
^(.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
它可以进行一些改进吗
,所以让我知道是否需要改进。
What about
it could use some refinement, so let me know if it needs to be improved.
它应该像这样简单:
so
Pattern.matches("ajax", str)
其中 str 是传入的 url 字符串。
It should be as simple as:
so
Pattern.matches("ajax", str)
where str is the url string being passed in.