正则表达式搜索组默认值

发布于 2025-01-12 17:59:42 字数 259 浏览 2 评论 0原文

我需要从 Google Analytics 内容组的路径中提取语言。 url 的格式为 /:language/rest_of_the_path,如果缺少 /:language 部分,则为默认语言(例如 en

我的正则表达式如下,

^/?(en|de|fr|)/

除了默认替换空白语言之外,它工作正常。

这只能通过正则表达式来实现吗?

I need to extract language from the path for Google Analytics Content Group. The format of the url is /:language/rest_of_the_path, if the /:language part is missing it is default language (ex. en)

My regex is follwing

^/?(en|de|fr|)/

It works fine exept for the default substitution for the blank language.

Can this be achived by regex only?

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

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

发布评论

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

评论(1

梦里寻她 2025-01-19 17:59:42

搜索此模式 ^/(?!en/|de/|fr/)
并替换为 /en$0

它使用负向前瞻,并且仅当在 url 开头找不到可识别的语言时才进行替换

Search for this pattern ^/(?!en/|de/|fr/)
And replace with /en$0

It uses negative lookahead and it replaces only when it doesn't find a recognized language at the beginning of the url

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