Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am trying to fix an issue with routing getting confused by introducing regular expression to match my route parameters. The path needs to be something like:

/accounts/MER005

or

/accounts/MER0050000

so I came up with this expression [A-Z]{3}\d{3}?:\d{4}. But it doesn't work. There are no errors, but when I navigate to the URL, it doesn't do anything.

Just to clarify, this is my state:

$stateProvider.state('accounts', {
    abstract: 'true',
    url: "/accounts",
    template: '<div ui-view></div>'
}).state('accounts.view', {
    url: "/{accountNumber:[A-Z]{3}\d{3}?:\d{4}}",
    views: {
        '@': {
            templateUrl: 'tpl/account/index.html',
            controller: 'AccountController',
            controllerAs: 'controller'
        }
    },
    resolve: {
        account: ['$stateParams', 'AccountService', function ($stateParams, accountService) {
            return accountService.get($stateParams.accountNumber);
        }]
    },
    data: {
        requireLogin: true,
        pageTitle: 'Account details'
    }
})

Does anyone know how to get it to work properly?

share|improve this question

I think the problem is the ':' character in your regex, and the '?' is not working as you expect. I think this regex will solve your problem: [A-Z]{3}\d{3,7}

You can try those regexes here.

share|improve this answer
    
[3,7] means any number tween 3 and 7 right? It has to be 3 or 7, so not 4,5 or 6 – r3plica yesterday

Figured it out. Turns out that ui-router doesn't like \d. You have to use the longer [0-9] instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.