3

I am currently trying to match the following URL

https://www.example.com/discover/exampleuser/review

there are other urls that i am matching with regex as well

https://www.example.com/discover/community
https://www.example.com/discover/reviews
https://www.example.com/discover/activity

I have the following code that will match the last three urls fine

<Route path={`${url}/(community|reviews|activity)`} render={prop => (
  <div className='mb-4'>
    <CollectionWidget { ...prop } />
  </div>
)} />

Whenever I use the following path it works

{`${url}/:username`}

however when i try to match all of the urls like so it does not

{`${url}/(community|reviews|activity|:username)`}

How do i match regex with a ambiguous id

2
  • Have you tried wrapping the wildcard in as a variable? {${url}/(community|reviews|activity|${:username})} Does that help? Commented Feb 8, 2019 at 3:28
  • 1
    so is match.params.user just silently ignored in target component? if there are if ... else ... inside the component it'd be better to refactor all that into 2 separate components bound to different(parametrized and without parameters) routes. Commented Feb 8, 2019 at 18:21

1 Answer 1

4

That sounds impossible to be honest, how could you match both unnamed and named parameter at the same time?

Good thing you could pass in 2 strings (or more):


<Route 
  path={[
    `${base}/(a|b|c)`, 
    `${base}/:username
  `]} 
  component={({ match }) => <div children={JSON.stringify(match.params)} />} />

// /b ---> { 0: "b" }
// /c ---> { 0: "c" }
// /d ---> { "username": "d" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.