I'm trying to create regex to check against a valid directory prefix, where directory names must not be empty, and can contain any alphabet characters [a-zA-Z] and any numbers [0-9]. They can also contain dashes (-) and underscores (_) but no other special characters. A directory prefix can contain any number of forward slashes (/), and a valid path must not end with a forward slash (/) either.
Valid examples would be:
/abc/def/hello_123/world-456/(a special case where a single forward slash is allowed)
Invalid examples would be:
/abc/def//abc//def//abc/def/abc/def/file.txt
So far I have ^(\/+(?!\/))[A-Za-z0-9\/\-_]+$ but it's not checking against what would be empty directory names (e.g. two forward slashed one after the other /abc//def), or path's ending with a slash (e.g. /hello_123/world-456/)
/+will match ALL slashes, so(?!\/)will always be true.