如何使用正则表达式来约束 ReactJS 路由



正如正则表达式中所说:匹配所有内容,但为了匹配除字符串之外的所有内容,您可以使用^(?!foo$).*

如图所示 https://regex101.com/r/9khSXB/2

但是在 ReactJS 路由中使用 https://www.npmjs.com/package/path-to-regexp 时,这不起作用(错误 ->无效路径(

<R.Route path="/^(?!/whatsmyname$).*" render={() => <div>works!</div>} />

(也在这里看到 http://forbeslindesay.github.io/express-route-tester/(

如何以节点友好的风格编写此正则表达式?

更新:这确实有效,您需要大括号

<R.Route path={/^/(?!whatsmyname$).*/} render={() => <div>works!</div>} />

我需要大括号

<R.Route path={/^/(?!whatsmyname$).*/} render={() => <div>works!</div>} />

最新更新