[英]JS Regex to match Chars with - or without -
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.这样做的目的是让 uris 进入我的路由器进行匹配。
for example I want to allow flexibility on az and allow any words to be separated since we might allow callers to send in seo-friendly values例如,我想允许 az 的灵活性并允许分隔任何单词,因为我们可能允许调用者发送 seo-friendly 值
/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.我正在使用koa-controller并试图想出一个正则表达式来实现这一点。 As far as I know I think koa-controller uses middleware path-to-regexp
据我所知,我认为 koa-controller 使用中间件路径到正则表达式
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.
我只是想为我的 koa-controller 路由创建一个带有正则表达式的路由,允许调用者发送允许名称的 url,而名称可以有破折号或没有。
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:
我要么没有正确使用正则表达式,要么没有正确地将正则表达式添加到 koa-route 中,或者同时错误,我不知道哪个:
'/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. /countries/united-states-of-america/states/illinois/cities/chicago
只是一个例子。 I'm just trying to get a route defined for my koa routes that works.我只是想为我的 koa 路线定义一条有效的路线。
the countryUrlFriendlyName for example are just named params that map to my controller action methods in koa-controller middleware.例如countryUrlFriendlyName只是命名参数,映射到我在 koa-controller 中间件中的控制器操作方法。 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.
所以 to: 部分意味着我要映射到一个以 # 命名的函数,那些 :[param name] 映射到。
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.
我的完整路线允许国家/[name]/states/name/cities/[name] 是我想允许他们发送带连字符或不带连字符的 [name],是的,连字符可能在那里,或随机或不存在.
I'm allowing our web team to request a match on some seo names they send into our API.我允许我们的网络团队请求匹配他们发送到我们 API 的一些 seo 名称。
united-?states-?of-?america
?
will make -
optional.将使
-
可选。
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.我假设您的问题是您有一个字符串
"unitedstatesofamerica"
并且您想要匹配该字符串,但是在随机位置中间有任意数量的连字符。
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-?/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.