Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 在特定路由上呈现组件-正则表达式路径_Regex_Reactjs_React Router_React Router V4_React Router Dom - Fatal编程技术网

Regex 在特定路由上呈现组件-正则表达式路径

Regex 在特定路由上呈现组件-正则表达式路径,regex,reactjs,react-router,react-router-v4,react-router-dom,Regex,Reactjs,React Router,React Router V4,React Router Dom,我试图仅在几个特定路径中呈现组件(标题),而不在其子路径中呈现,即/foo而不是/foo/bar中 假设我有一个路径数组: 让路径=['/foo','/bar','/baz'] 我试图这样做,但没有成功: const x = paths.map(e => `(${path}${e}/?$)`) <Route path={x.join('|')} component={this.makeMyComponent} /> cons

我试图仅在几个特定路径中呈现组件(标题),而不在其子路径中呈现,即
/foo
而不是
/foo/bar

假设我有一个路径数组:

让路径=['/foo','/bar','/baz']

我试图这样做,但没有成功:

    const x = paths.map(e => `(${path}${e}/?$)`)
    <Route
      path={x.join('|')}
      component={this.makeMyComponent}
    />
const x=path.map(e=>`(${path}${e}/?$)`)
这也不起作用:

    const x = paths.map(e => `(${e}/?$)`)
    <Route
      path={`${path}(/foo$|/bar$|/baz$})}
      component={this.makeMyComponent}
    />
const x=path.map(e=>`(${e}/?$)`)
但它确实与在线正则表达式引擎匹配:

这起作用了:

    <Route
      render={props => paths.some(e => props.location.pathname.endsWith(e)) ? this.makeMyComponent() : null}
    />
path.some(e=>props.location.pathname.endsWith(e))?this.makeMyComponent():null}
/>