如何模糊匹配路由路径

问题描述 投票:0回答:1

我正在使用 React Router v5,我想知道如何在一条路径中同时匹配

/add_a
/add

我已经尝试过

<Route path="/add*" element={<div>TEST</div>} />
,但我收到警告
Route path "/add*" will be treated as if it were "/add/*"

<Routes>
 <Route path="/add*" element={<div>TEST</div>} />
</Routes>

参考: https://v5.reactrouter.com/web/api/Route/path-string-string

reactjs react-router
1个回答
0
投票

试试这个

/add[^/]+
这将匹配除第二个“/”之外的所有内容

这意味着

/add_somethinghere
匹配

但是

/add_somethinghere/extrathing
不匹配

尝试这里的游乐场https://forbeslindesay.github.io/express-route-tester/

© www.soinside.com 2019 - 2024. All rights reserved.