Guig
Guig

Reputation: 10387

js regex to match path that starts with X and then not Y

I'd like to match

/.well-known/*

Except

/.well-known/apple-app-site-association

I've tried a few things around

\/\.well-known\/^(?!apple-app-site-association).*

but they don't match strings like

/.well-known/123/erg3rg
/.well-known/

:

var reg = new RegExp(/\/\.well-known\/^(?!apple-app-site-association).*/)
"/.well-known/apple-app-site-association".match(reg) // null :)
"/.well-known/123".match(reg) // null :(

Upvotes: 0

Views: 102

Answers (2)

Piyush
Piyush

Reputation: 1162

This should work.

  \/\.well-known\/(?!apple-app-site-association).*

I just removed the ^.

Upvotes: 1

Brian Stephens
Brian Stephens

Reputation: 5271

You're on the right track, but you have an extraneous ^. It should be:

\/\.well-known\/(?!apple-app-site-association)

The caret is to match the beginning of the text, so it will never match in the middle of your regex.

Note: the .* at the end was also extraneous, since it would match all the same with or without it.

Upvotes: 3

Related Questions