匹配一个或另一个字在AngularJS ui-router

时间:2021-09-20 04:40:39

Using AngularJS + ui-router, I need to match a url with one of two words:

使用AngularJS + ui-router,我需要将url与以下两个词中的一个匹配:

i.e. /oneWord/... or /secondWord...

例如/ oneWord /…或/ secondWord……

The routing and controllers are nearly identical, so I want to avoid duplicating code.

路由和控制器几乎是相同的,所以我想避免重复代码。

However, using a regex I'm used to, doesn't work where regular expressions should work:

然而,使用我习惯的正则表达式在正则表达式应该工作的地方并不适用:

url: '/{type:(oneWord|secondWord)}/:group',

url:' / {类型:(oneWord | secondWord)} /:组织”,

However, it throws an error:

然而,它抛出了一个错误:

Unbalanced capture group in route '/{type:(oneWord|secondWord)}/:group'

路径'/{type:(oneWord|secondWord)}/:group'

A regex should work though, because this works fine:

regex应该可以工作,因为它可以工作:

url: "/{page:[0-9]{1,8}}"

url:" / {页面:[0 - 9]{ 1,8 } }”

I know that (oneWord|secondWord) is a valid regex, so what have I missed?

我知道(一个单词|secondWord)是一个有效的regex,那么我漏掉了什么?

1 个解决方案

#1


23  

According to the documentation you should not use capturing parentheses in your regex patterns.

根据文档,您不应该在regex模式中使用捕获括号。

This should work: url: '/{type:(?:oneWord|secondWord)}/:group'

这应该可以工作:url: '/{type:(?:一个单词|secondWord)}/:group'

#1


23  

According to the documentation you should not use capturing parentheses in your regex patterns.

根据文档,您不应该在regex模式中使用捕获括号。

This should work: url: '/{type:(?:oneWord|secondWord)}/:group'

这应该可以工作:url: '/{type:(?:一个单词|secondWord)}/:group'