Correspondence to a particular word in AngularJS ui-router

Using AngularJS + ui-router, I need to map the URL to one of two words:

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

Routing and controllers are almost identical, so I want to avoid code duplication.

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

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

However, this causes an error:

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

A regex should work because it works fine:

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

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

+7
angularjs regex
source share
1 answer

As per the documentation, you should not use parentheses for parentheses in your regular expression patterns.

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

+23
source share

All Articles