用于匹配 URL 并插入目录的正则表达式

发布于 2025-01-03 06:14:51 字数 278 浏览 0 评论 0原文

我想使用正则表达式来匹配以下内容:

http://www.test.com/example/sometext/

然后重定向到:

http://www.test.com/uk/example/sometext/

其中 'example' 不在保留字列表中,例如 _images_lib_css 等。

I would like to use regex to match the following:

http://www.test.com/example/sometext/

and then redirect to:

http://www.test.com/uk/example/sometext/

where 'example' is not in a list of reserved words, like _images, _lib, _css, etc.

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

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

发布评论

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

评论(1

若能看破又如何 2025-01-10 06:14:51

使用否定的前瞻:

(http://www.test.com/)((?!(_images|_lib|_css))[^/]+/sometext/)

并替换为

$1uk/$2

Broken down,有趣的是:

  • (?!someregex) = 否定的前瞻 - 即断言以下输入与 someregex 不匹配
  • (_images|_lib|_css) = 正则表达式 OR 逻辑的语法,仅使用文字
  • [^/]+ = 某些字符t 斜线

Use a negative look ahead:

(http://www.test.com/)((?!(_images|_lib|_css))[^/]+/sometext/)

And replace with

$1uk/$2

Broken down, the juicy buts are:

  • (?!someregex) = a negative lookahead - ie assert the following input does not match someregex
  • (_images|_lib|_css) = the syntax for regex OR logic, just using literals
  • [^/]+ = some characters that aren't a slash
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文