Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试创建一个匹配 URL 路由的正则表达式片段。
基本上,如果我有这条路线,/users/:id我想/users/100匹配,但/users/100/edit不匹配。
/users/:id
/users/100
/users/100/edit
这就是我现在正在使用的:users/(.*)/但由于贪婪匹配,无论用户 ID 后面是什么,它都会匹配。/edit如果路线的尽头有一个或其他东西,我需要一些“打破”比赛的方法。
users/(.*)/
/edit
我研究了 Regex NOT 运算符,但没有运气。
有什么建议吗?
你只是想收集数字吗?
你可以使用users/(\d*)/
users/(\d*)/
如果您想收集直到 a 的所有内容/,并且它使用 NOT,这就是您将如何做到的,^/users/[^/]*$
/
^/users/[^/]*$
您可以使用负前瞻:
users/(.*)/(?!edit)
然而,这总是需要一个斜杠。也许更好的解决方案是:
users/(\d+)(?!/edit)
有关更多信息,请参阅此帖子。