我正在使用regex而不是string将页面路由到404 react-router path prop
我有一个不同路线的列表,格式如下:
const withdrawRuote = 'user/:userId/withdraw' const depositRoute = 'user/:userId/deposit'
const groupNewRoute = 'group/new' const groupWithdrawRoute = 'group/withdraw' const groupDepositRoute = 'group/deposit'
对于路线1
Route(exact path=myRegex1 render=404)
myRegex1
使用上述regex,应通过以下操作:
应该失败
对于路线2
Route(exact path=myRegex2 render=404)
myRegex2 . 这些应该通过:
如何让regex知道我需要这些词 deposit withdraw 或 user 作为一个词而不仅仅是一组字符,考虑到我是排除它们而不是包括它们。
deposit
withdraw
user
您需要告诉regex引擎,您只想避免将url与 withdraw deposit 最后:
^\/user\/?[0-9]+\/(?!(?:deposit|withdraw)\/?$).*$ ^^^^
见 regex demo
(?!(?:deposit|withdraw)\/?$) 一旦匹配(立即位于当前位置的右侧),负lookahead将使匹配失败:
(?!(?:deposit|withdraw)\/?$)
(?:deposit|withdraw)
撤退
\/?
/
$