如何选择直到并包括第 3 个斜线 (RegExp) 的所有内容? [英] How to Select everything up to and including the 3rd slash (RegExp)?

查看:15
本文介绍了如何选择直到并包括第 3 个斜线 (RegExp) 的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第 3 个斜杠之前选择所有的最简单的 RegExp 是什么?

What would be the easiest RegExp selecting all before the 3rd slash?

我试过了:

([^/?#]+){3}(?:.*?/)

但它并不像我希望的那样工作.更重要的是我不知道它会在 Google Analytics(过滤器部分)中工作

But it does not work exactly as I would hope it would do. What's more I don't know that it will work in Google Analytics (Filter Section)

我希望它匹配:

  1. /
  2. /news/
  3. /news/details/

除此之外的所有东西(在第三个斜线之后)都不是我想要的

Everything more than this (after the 3rd slash) is not what I want to get

推荐答案

在您的评论中,您给出了三个示例://news//新闻/详情/.基于这些例子,这里有一个解决方案:

In your comment, you give three examples: /, /news/ and /news/details/. Based on these examples, here is a solution:

^(/[^/]*){1,2}/?

它说:从字符串的开头匹配以下内容:

It says: from the start of the string match the following:

  • 一个斜线,然后是零个或多个非斜线
  • 可选后跟第二个斜线加上零个或多个非斜线
  • 可选后跟最后的斜线

这篇关于如何选择直到并包括第 3 个斜线 (RegExp) 的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆