skip to Main Content

I want to be able to match either of the following:

unitedstatesofamerica
united-states-of-america

so match on chars with or without dashes, allow either.

the purpose of this is so I can allow uris coming into my router to match on.

for example I want to allow flexibility on a-z and allow any words to be separated since we might allow callers to send in seo-friendly values

/countries/united-states-of-america
/countries/unitedstatesofamerica
/countries/germany
/countries/stlucia
/countries/st-lucia

etc.

I’m using koa-controller and trying to come up with a regex route to allow this. As far as I know I think koa-controller uses middleware path-to-regexp

What I’m asking is not unclear to whoever didn’t understand. I’m simply trying to create a route with regex for my koa-controller routes that will allow callers to send in urls that allow names whereas the names could have dashes or not.

Here’s what I tried with no luck. I’m either not getting the regex right or syntax right with how you add a regex to a koa-route or both wrong at the same time, I don’t know which:

'/countries/:countryUrlFriendlyName(/^[a-z-]+$/i)/states/:stateUrlFriendlyName(/^[a-z-]+$/i)/cities/:cityUrlFriendlyName(/^[a-z-]+$/i)': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'}

'/countries/:(/^[a-z-]+$/i)/states/:(/^[a-z-]+$/i)/cities/:(/^[a-z-]+$/i)': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'}

'/countries/(/^[a-z-]+$/i)/states/(/^[a-z-]+$/i)/cities/(/^[a-z-]+$/i)': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'},

'/countries/:countryUrlFriendlyName/^[a-z-]+$/i/states/:stateUrlFriendlyName/^[a-z-]+$/i/cities/:cityUrlFriendlyName/^[a-z-]+$/i': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'},

'/countries/:countryUrlFriendlyName/^[a-z-?]+$/i/states/:stateUrlFriendlyName/^[a-z-?]+$/i/cities/:cityUrlFriendlyName/^[a-z-?]+$/i': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'},

'/countries/:countryUrlFriendlyName(/^[a-z-?]+$/i)/states/:stateUrlFriendlyName(/^[a-z-?]+$/i)/cities/:cityUrlFriendlyName(/^[a-z-?]+$/i)': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'},

'/countries/:/^[a-z-]+$/i/states/:/^[a-z-]+$/i/cities/:/^[a-z-]+$/i': {to: 'city#findByCountryAndStateAndCityUrlFriendlyName'}

all these were attempts to get this working above, I tried all those different route definitions and none would match on for example an incoming request of:

/countries/united-states-of-america/states/illinois/cities/chicago just as an example. I’m just trying to get a route defined for my koa routes that works.

the countryUrlFriendlyName for example are just named params that map to my controller action methods in koa-controller middleware. I want to allow people to send in values for those params with dashes or not.

I have a controller which these params are mapped to. So the to: part means I’m mapping to a function named after # that those :[param name] maps to.

And as you can see there’s more to the story, the route is a bit longer but I was trying to concentrate on just trying to get the regex working for country as an example. My full route allows countries/[name]/states/name/cities/[name] and it’s the [name] which I wanna allow them to send in with hyphens or not and yes the hyphens could be there, or random or not there.

I’m allowing our web team to request a match on some seo names they send into our API.

2

Answers


  1. united-?states-?of-?america
    

    ? will make - optional.

    Login or Signup to reply.
  2. I assume your problem is that you have a string "unitedstatesofamerica" and you want to match that string, but with any number of hyphens stuck in the middle in random places.

    In that case, you will have to build a regexp yourself, which you can do by

    function make_regexp_with_inserted_hyphens(str) {
      return new RegExp(str.split('') . map(chr => chr + "-?") . join(''));
    }
    
    make_regexp_with_inserted_hyphens("unitedstatesofamerica")
    /u-?n-?i-?t-?e-?d-?s-?t-?a-?t-?e-?s-?o-?f-?a-?m-?e-?r-?i-?c-?a-?/
    

    To match any of a number of words:

    function make_multi_regexp_with_inserted_hyphens(strings) {
      return strings.map(make_regexp_with_inserted_hyphens) . join('|');
    }
    
    make_multi_regexp_with_inserted_hyphens("germany", "stlucia")
    /g-?e-?r-?m-?a-?n-?y-?/|/s-?t-?l-?u-?c-?i-?a-?/
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search