每当 URL 中有大写字母时,反应路由器重定向



我想解决每当 URL 中有大写字母需要重定向到小写 URL 时。

我在路径部分找到正确的正则表达式时遇到了一些问题。

正则表达式/:url2*([A-Z](/仅在所有字母都为大写时才有效,而不是在这种情况下/home/Support/

我在这里尝试了不同的路径 https://pshrmn.github.io/route-tester 没有任何运气。

这是我的代码

<Route
exact
strict
sensitive
path='/:url2*([A-Z])/'
render={props => {
const path = props.location.pathname
return <Redirect to={`${path.toLowerCase()}`} />
}}
/>

我不是专家,但看看这是否适合您。

<Route
exact
strict
sensitive
path='/:url([a-z/]*[A-Z]+[a-z/]*)/'
render={props => {
const path = props.location.pathname
return <Redirect to={`${path.toLowerCase()}`} />
}}
/>

正则表达式/:url([a-z/]*[A-Z]+[a-z/]*)/检查 URL 中是否至少有一个大写字母。

敏感道具应该在不需要自定义渲染道具的情况下解决这个问题:

<Route
exact
strict
sensitive={false}
path='/:url([a-z/]*[A-Z]+[a-z/]*)/'
/>

最新更新