0

I have a regex pattern I need to match in ui-router (docs). I have a regexp tested outside of angular ready:

(?=.*\d)(?=.*[a-zA-Z0-9])\w{8}

I need 8 characters, one of which is a digit in any position.

When I insert it into my angular app, I get routing errors. Here's my code:

var accountNumberParam = '{accountNumber:(?=.*\d)(?=.*[a-zA-Z0-9])\w{8}}',
var states = [
    {
            name: 'name',
            url: '/' + accountNumberParam,
            templateUrl: '/path/to/my.html',
            controller: 'MyCtrl'
     },
]

I've tried with and without leading/trailing "/" characters. I think this is a formatting issue but I can't find anything different about the way this is formatted and the documentation. Thanks for the help.

2
  • You tried making it an angular constant and injecting it and using it from there? Commented Apr 18, 2016 at 16:08
  • Nope, turning it into a constant doesn't change anything Commented Apr 18, 2016 at 16:48

1 Answer 1

1

You need to make sure that your regex string is properly escaped.

This is because in this instance you are not using the Javascript regex primitive, but instead relying on Strings.

When storing your Regex as a string value you need to make sure that you escape twice to ensure that there are not any unintentional escapes.

var accountNumberParam = '{accountNumber:(?=.*\\d)(?=.*[a-zA-Z0-9])\\w{8}}';
var states = [{
  url: '/' + accountNumberParam,
  template: '<h1>TEST : {{accountNumber}}</h1>',
  controller: testController,
}];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.