16
votes

I'm trying to match:

/app
/app/
/app/**

but not:

/app**

The trailing slash should be optional. I can't quite get it to work.

^/app/([^/]*)/? matches /app/ and /app/**, but not /app.

5
** means any path like in /app/** could be /app/a/path/to/something ? Or ** literally ? - Ludovic Kuty
yes, any path would match (like your example of /app/a/path/to/something). is there some kind of regex that says "either end here or have this character (/) and anything else. I tried ends with in an or but that didn't work either. - MoatMonster

5 Answers

13
votes

Try this:

^\/app((\/\w+)+|\/?)$
11
votes

This will work:

^/app(/[^/]*)?

edit: This will also work:

^/app(/.*)?
2
votes

Using this in production right now:

^\/app((\/.*$)|$)

Here's an example of it on RegExr

https://regexr.com/41bo1

0
votes

Have you tried this:

   ^/app(/[^/]+)*/?

I'not sure about the ** - thing, but can you really be needing something like this ???

   ^/app/?(/[^/]+)*/?$
0
votes
^/(app1|app2)(?:/(.*))?$

matches all the below

/app1 or /app1/ or /app1/whatever
/app2 or /app2/ or /app2/whatever

^/app(?:/(.*))?$

will match
/app or /app/ or /app/whatever