Too long for a comment, so I'm posting this here
The title of the answer is confusing. You tell about checking some valid directory. But reading the comments a more appropiate title would have been something like "Validate custom rules against a path-like string".
Where your rules seem to be (according to the comments)
- It may start with any (or 0) number of
../
- Probably (need confirmation) you don't want to allow standalone
.
or ..
wihin the rest of the directory (excluding prefix)
- The path (excluding prefix) may start as
text
or /text
- Probably (need confirmation) you don't want to allow 2 or more consecutive slashes
If so, this may work for you: ^(\.\.\/(?:\.\.\/)*)?(?!.*?\/\/)(?!(?:.*\/)?\.+(?:\/|$)).+$
See demo