Reputation: 11
I have a url that I want to route to with React router. The id at the end is the part that changes.
https://mywebsite.com/p/foo-bar-fizz-buzz-myid1234
What is the correct regular expression that I need to use in react router? Right now I have the following.
<Route path="(p\/(.*-))":myId component={myComponent} />
Its my understanding that I need to grab the values after the 'p/' up to and including the last dash. I've seen answers involving look arounds but those arent supported in Javascript. How can I accomplish this?
Upvotes: 1
Views: 237
Reputation: 8407
From what I understood, you do want to match everything until the last dash.
That can be accomplished with this expression.
/\/p\/.+\-/
-
, so it will stop the previous expression here. React Router uses path-to-regexp
so your code should work with this solution
<Route path="(p\/(.+\-)):myId" component={myComponent} />
Upvotes: 1