Reputation: 2940
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.
Upvotes: 0
Views: 50
Reputation: 1240
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,
}];
Upvotes: 1