Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regex in path? #391

Closed
jenil opened this issue Oct 12, 2014 · 11 comments
Closed

Regex in path? #391

jenil opened this issue Oct 12, 2014 · 11 comments

Comments

@jenil
Copy link

jenil commented Oct 12, 2014

Hello,

Is there a provision for something like <Route name="EventListing" path={/\/(movies|concerts|plays)/} handler={EventsList} />?

@ryanflorence
Copy link
Member

Nope. Just make three routes that use the same handler.

@phun-ky
Copy link

phun-ky commented Jul 8, 2016

Hm, this is an old issue, but in my current team we have this issue:

<Route path={'/apples/:appleId'} component="AppleComponent"/>
<Route path={'/apples/:appleId/otherstuff/**/'} component="OtherStuffComponent"/>
<Route path={'/apples/:fruidId/someotherstuff/**/'} component="SomeOtherComponent"/>
<Route path={'/apples/:fruidId/someotherstuff/**/'} component="SomeOtherOtherComponent"/>

or

<Route path={'/apples} component="ApplesComponent">
  <Route path={':appleId'} component="AppleComponent">
    <Route path={'otherstuff/**/'} component="OtherStuffComponent"/>
  </Route
  <Route path={':fruidId/otherstuff/**/'} component="SomeOtherComponent"/>
  <Route path={':fruidId/otherstuff/**/'} component="SomeOtherOtherComponent"/>
</Route

The first one works, but not providing a meaningful path for packages/components relying on the path hierarchy, for example react-breadcrumbs. The last one makes :appleId greedy

If the path had regex support, we could solve this in a better way (with correct hierarchy)

@sassanh
Copy link

sassanh commented Aug 7, 2016

@ryanflorence sorry for mentioning you here. But as this thread is closed I just wanted you to notice it again or maybe reopen it. There are things that can be done easily by regex and to so with current wildcard used in react-router developer needs hacks and duplicating code, etc.

I guess you're aware of this but let me know if we need a list of things that current wildcard system fails to do and regex can. If we try to expand its functionality as discussed here #142 we'll end up reinventing regex. I think best would be to support regex just like Django does for example: https://docs.djangoproject.com/en/1.10/topics/http/urls/

@jalooc
Copy link
Contributor

jalooc commented Apr 2, 2017

For me too, dealing with path not being able to accept a regex is a pain for it enforces some hackish solutions for things that could be handled with regex in one line. What is more, the docs suggest, that path can be anything that path-to-regexp understands, so I guess regex should be included.

The most interesting part is, as far as I can see, that path does work with regex, only the PropType is string. @ryanflorence How about changing the PropType then? :)

EDIT: Something like this #4900 might do? The tests pass and it works in my cases, not sure if it's okay for other dependent packages, like the native routing for example.

@harlantwood
Copy link

harlantwood commented Apr 14, 2017

react router 4 support regex paths, eg:

<Route exact path="/commit/:git_commit([0-9a-fA-f]{40})" component={ViewCommit} />

@albertstill
Copy link

albertstill commented Sep 19, 2017

I don't think it does support Regex, the link above requires payment, and I only see path-to-regexp in the source.

A React Router 4 solution using string paths and a Regexp in the render prop can look like this:

<Switch>
  { ...other routes }
  <Route
    path="/:slug"
    exact
    strict
    render={({ match }) => {
      if (!/^[A-Za-z0-9]{6}$/.test(match.params.slug)) {
        return <NotFound />;
      }
      return <CommitmentPage />;
    }}
  />
  <Route component={NotFound} />
</Switch>

Here /about-us does not match but /fU3oE8 does.

@sassanh
Copy link

sassanh commented Sep 19, 2017

React Router 4 does support regex. He provided an example apart from the link.

@albertstill
Copy link

albertstill commented Sep 19, 2017

Ah oops nice one, the path-to-regexp library React Routers uses supports custom match parameters in the string. Thanks

maks-rafalko added a commit to maks-rafalko/react-router that referenced this issue Dec 27, 2017
timdorr pushed a commit that referenced this issue Dec 27, 2017
* Example with regex in path

Related to #391 and this example #391 (comment)

* Add comment to better explain how regexes work with path
joybro added a commit to joybro/Cowiki that referenced this issue Jan 22, 2018
This wiki supports hierarchy b/w articles. For example, an article 'ab/cd' is under
the 'ab' article conceptually. For this purpose, article id need to be
able to include slash.

References:
- remix-run/react-router#391 (comment)
- https://stackoverflow.com/questions/10020099/express-js-routing-optional-spat-param/14481168#comment49246489_29549148
bongsok pushed a commit to bett-io/Cowiki that referenced this issue Jan 23, 2018
This wiki supports hierarchy b/w articles. For example, an article 'ab/cd' is under
the 'ab' article conceptually. For this purpose, article id need to be
able to include slash.

References:
- remix-run/react-router#391 (comment)
- https://stackoverflow.com/questions/10020099/express-js-routing-optional-spat-param/14481168#comment49246489_29549148
@reverofevil
Copy link
Contributor

@timdorr Can we please remove the shameless ad up there with paywalled article on egghead?

@Mindaugas-Jacionis
Copy link

Hi, any option for negative regexp. I have 2 routes /subscribe/:token && /subscribe/success, I would like to have something: /subscribe/:token(*|!success) to exclude success from route with :token. Otherwise I get the one with token param rendered & not the one for the success.

I've tried this: /subscribe/:token((?!success).*), but got some crazy error.

@u-rogel
Copy link

u-rogel commented Mar 3, 2018

@Mindaugas-Jacionis I guess it is not the solution you wanted but will work.

    <Route path="/subscribe/success" exact component={Success} />
    <Route path="/subscribe/:token" exact component={Token} />

When you place it like this in the switch component, the success path will be matched before the :token path is.

@remix-run remix-run deleted a comment from kalimk Apr 27, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jun 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants