-2

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.

PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
  • well I'm trying to add this to koa-controller routes and so far no luck... – PositiveGuy Oct 04 '15 at 04:53
  • So the regular expression must only match the string "the-united-states-of-america" whether the words are separated by a hyphen ("-") or not separated at all? Or are other words/phrases also expected to be matched? – David Thomas Oct 04 '15 at 04:58
  • no meaning the user can send in any text and it may be separated by dashes or it may not. So they could send in unitedstatesofamerica or I can allow united-states-of-america – PositiveGuy Oct 04 '15 at 05:08
  • dashes could be anywhere, and I should allow dashes and a-z characters..any combination of both or whatever. Or again they might just send in just a-z and no dashes, that's fine too – PositiveGuy Oct 04 '15 at 05:09
  • yes words and phrases allowed, can be separated by dashes or not – PositiveGuy Oct 04 '15 at 05:09
  • this is to allow for my url such as /countries/united-states-of-america or /countrires/unitedstatesofamerica stuff like that – PositiveGuy Oct 04 '15 at 05:10
  • Then you need to [edit] your question to clarify the requirements; as it is it's written very vaguely, and is open to interpretation and guess-work; this leads to less-specific, and less-useful, answers for you: help us to help you, be specific. – David Thomas Oct 04 '15 at 05:13
  • if the position of the hypen is totally random, best solution is the one of **torazaburo** – winner_joiner Oct 04 '15 at 05:26
  • This question is incomprehensible. If the goal is to match any sequence of letters or hyphens, then @Avinash Raj's solution works. In that case, why was it necessary to post a question in the first place for one of the simplest regexp problems in human history? Or is the problem to allow any one of a predetermined set of words, but with the twist that there may be intervening hyphens? –  Oct 04 '15 at 05:31

2 Answers2

3
united-?states-?of-?america

? will make - optional.

vks
  • 67,027
  • 10
  • 91
  • 124
0

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-?/
  • I know. That's why `make_regexp_with_inserted_hyphens` takes a parameter, for you to pass in whatever string you want. –  Oct 04 '15 at 05:13
  • so using koa-controller which uses koa-router which uses path-to-regexp any help in getting my route defined? I've tried so many things. I can't really use a function with this when defining routes for koa-controller. – PositiveGuy Oct 04 '15 at 05:29
  • You can easily precompute the regexp outside koa, then pass it in. –  Oct 04 '15 at 05:33
  • where would I intercept the request and do so? the http request hits the routes, I don't control anything until I get the koa context within my controller action that the route passes the context to – PositiveGuy Oct 04 '15 at 05:44
  • and I still have the issue of how to format the route correctly using the regex... – PositiveGuy Oct 04 '15 at 05:45