Regex match URL with exactly one folder deep

Anthony 'Ants' Nguyen

Trying to figure out how to handle this Regex but cant seem to get the correct sequence.

www.abc.com EXCLUDE 
www.abc.com/some-folder-01 MATCH
www.abc.com/some-folder-01?someQScontent MATCH
www.abc.com/some-folder-01/some-product-01 EXCLUDE
www.abc.com/some-folder-02/some-product-01?someQScontent EXCLUDE

some-folder-01 could be any number of letters, numbers or hyphens. I've been using [\w-]+ to get the some-folder-01 but I haven't figured out how to say NOT succeeded by another forward slash and another folder. I tried (!?/) but then that just reduced some-folder-01 match down to some-folder-0

As it stands my regex looks like: www.abc.com/([\w-]+)

anubhava

You can use this regex:

/www\.abc\.com\/[^/]+[?\/#]?$/

RegEx Demo

Explanation:

  • www\.abc\.com\/: Match literal string www.abc.com/
  • [^/]+: Match 1+ non-slash characters
  • [?\/#]?$: Match optional / or ? or # followed by end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事