user247702
user247702

Reputation: 24232

Regex route constraint with negative lookahead

Tested with http://regexhero.net/tester/

^(?!Bar$)

When setting a route constraint, I have to use ^(?!Bar$).* to get the same results. Why is that?

Upvotes: 1

Views: 575

Answers (1)

raina77ow
raina77ow

Reputation: 106483

The first expression actually doesn't 'cover' any part of string. See, both ^ and (?!...) sub-expressions match at specific positions (anchors) in the tested string (actually they both try to match at the same position - right at the beginning - and fail immediately).

And this - ^(?!Bar$).* - actually 'covers' all the string. I suppose that's the difference.

Upvotes: 3

Related Questions