-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.

4

2 回答 2

3
united-?states-?of-?america

?将使- 可选。

于 2015-10-04T04:41:24.893 回答
0

我假设您的问题是您有一个字符串"unitedstatesofamerica"并且您想要匹配该字符串,但是任意数量的连字符卡在随机位置的中间。

在这种情况下,您必须自己构建一个正则表达式,您可以通过

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-?/

要匹配多个单词中的任何一个:

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-?/
于 2015-10-04T05:06:29.730 回答