filter slugs (url) with regex

Viewed 101

Im new to regex so I may have made some stupid error

goal:

I am looking for a regex which:

  1. matches: /saj/
  2. fails at: /asdf/adsf/

context:

For my markdown site built with gatsby I need to filter al submenus (/x/) from subpages (/x/x/).

my attempts:

1 Answers

Using this pattern (\/.*?\/) will get a partial match, but it would also match // as the quantifier is optional. Note that you also don't need the capture group.

Using this pattern (\/.*?)(?!\/{2,}) captures a single / followed by as least as possible chars asserting not 2 or more / at the right.


You can match the whole line using anchors and match at least a single char other than / in between using a negated character class [^\/]+

^\/[^\/]+\/$

Regex demo

Related