Sparkup
Sparkup

Reputation: 3754

Regex match trailing slash except pattern

I'm looking for strings that end with / but not when the string is equal to /[a-z]{2}/ (with the 2 slashes in the pattern)

To exclude the unwanted string I would use :

(?!/[a-z]{2}/)

For strings ending in a slash I'd use :

.*/$

However, my limited knowledge with regular expression doesn't allow me to combine the two patterns. How would I do this ?

This would match :

/en/contact/

This wouldn't :

/en/

Upvotes: 3

Views: 2889

Answers (2)

anubhava
anubhava

Reputation: 784998

This regex should work:

^(?!.*?\/[a-z]{2}\/$).*?\/$
Online Demo

Upvotes: 1

xdazz
xdazz

Reputation: 160833

The regex should work for you:

^(?!\/[a-z]{2}\/).*\/$

You could check it here.

Update for your added requirement:

^(?!^\/[a-z]{2}\/$).*\/$

The demo.

Upvotes: 4

Related Questions