Path negation

Author: blakeembreyCreated Jun 11, 2025Updated Jul 14, 2025

It comes up often that people want to be able to exclude a route from matching: https://github.com/pillarjs/path-to-regexp/issues/378, https://github.com/pillarjs/path-to-regexp/issues/268, https://github.com/pillarjs/path-to-regexp/issues/252, https://github.com/pillarjs/path-to-regexp/issues/99, and more.

With the removal of direct regex input in v8 it's no longer possible to use the workaround of ((?!login).*). To create a simpler version of this feature, I'm proposing adding new syntax to v8 to support it.

Proposal 1: Add regex params back from < v8

Allow (?!...) directly again.

Pros: "Just works" Cons: Hard to ensure it'll remain safe and secure from ReDoS. Hard to understand, kind of magic if you aren't familiar with regex.

Proposal 2: Add simplified regex params with a new syntax

Combine regex parameters and a meta character like ! to indicate negation. For example, :path!(login).

Pros: Generate safe regex. Can be clearer to read. Cons: It's either a positive or negative match, can't combine a|b|c|!login or something.

Proposal 3: Add meta character to existing path

Combine path features with a meta character. For example, /api{!/login}.

Pros: No need to re-introduce regex features. Cons: Very unclear what happens in the example. It would technically only match /api, and you need something confusing like /api{!/login}{/*path} which is a mess to read.

Recommendation

Proposal 2 is the most straightforward since regex is already planned to be re-introduced. Feel free to add other possibilities.