Matching url path with regex in $urlRouterProvider.when()

angular-ui-router, angularjs

Solution

I found the answer. Order of the when() statements and the state definition is important.

This works:

$urlRouterProvider
    .when('', '/events')
    .when('/', '/events')
    .when('/results', '/events/results')
$stateProvider
    .state('events', {
    ......
    }
$urlRouterProvider // This needs to be at the end to match all other matches
    .when('/:eventName', '/events/:eventName')

Problem

Based on the angular ui-router wiki (https://github.com/angular-ui/ui-router/wiki/URL-Routing#urlrouterprovider) it should be possible to use regex for matching the incoming path. How could I express regex to match following redirection rules: ``` $urlRouterProvider .when('', '/events') .when('/', '/events') .when('/:eventName', '/events/:eventName') .when('/results', '/events/results') ``` Test example: ``` localhost => localhost/#/events localhost/ => localhost/#/events localhost/myEvent => localhost/#/events/myEvent localhost/results => localhost/#/events/results localhost/#/events => localhost/#/events localhost/#/results => localhost/#/results ```

Original source