Reputation: 3
I am using this Regex as a filter to capture all of my pages that have a trailing slash (as opposed to their identical non-trailing slash versions)
^(/[a-z0–9/_\-]*[/])$
I see that it filters exactly what I want except for the homepage which is "/"
in google analytics. How can I include all of my pages /mypage/
, /features/
, /blog/this-is-my-blog-title
in the report and exclude their alter egos of /mypage
, /features
, /blog/this-is-my-blog-title
in the expression?
Upvotes: 0
Views: 1727
Reputation: 2563
You can use this regular expression:
\/(?:[\w-]+\/)*
Check it out at regex101.com, which also includes an explanation of how the regex will perform the matching.
Upvotes: 1
Reputation: 2342
To get the same exact meaning as your current regex, but allow a single /
, just add it in as an "or" option. The "or" character in regex is |
. So something like this would work:
^(/[a-z0–9/_\-]*[/]|/)$
One minor improvement you can make is to take the third /
out of the character class, since it is only one character. You might as well just match it literally. You also don't need to escape the -
, since it is at the end of the character class:
^(/[a-z0–9/_-]*/|/)$
Now this still has the same exact meaning as your original regex, and it also captures a single /
. There are a lot of other conditions in this regex, though. If you just want to check for any string ending in a /
, you can shorten it way down to this:
^.*/$
This will match any string that ends in a /
, including just a single /
. It also happens to be much shorter and a little bit faster than your original regex. Here's a breakdown of this updated regex:
^
This match has to start at the start of the string.*
Match 0 or more of any characters/
Match a literal /
. This ensures that the string ends in a /
.$
This match has to end at the end of the string. This ensures that the /
was actually at the end of the string, and not halfway through.Upvotes: 1
Reputation: 50220
Your homepage path is one character long, so simply replacing *
with +
in your regex will exclude it:
^(/[a-z0–9/_\-]+[/])$
But to match paths ending in /, I’d write just
^.+/$
Upvotes: 0