RobbTe
RobbTe

Reputation: 405

Regex: match URLs except some?

I would like to match all URLs, except some. I have the regex below. This excludes URLs that end with .html, but I also want to exclude URLs that have (somewhere) 'sub-cat' or 'sub-cat2' in it. This is however not working currently. What is going wrong? Thanks!

/webshop/category/.*+$(?<!\.html|sub-cat|sub-cat2)

Upvotes: 1

Views: 141

Answers (1)

anubhava
anubhava

Reputation: 785068

You may use a negative lookahead condition with alternations:

^(?!.*(sub-cat|sub-cat2|\.html$).*/webshop/category/.+$

RegEx Demo

(?!.*(sub-cat|sub-cat2|\.html$) will fail your match if URL ends with .html or if it has sub-cat or sub-cat2 anywhere.

Upvotes: 1

Related Questions