As an attribute of an input element I have:
ng-pattern="^\d{5}(?:[-\s]\d{4})?$"
Whats wrong this this expression?
I get this error:
Lexer Error: Unexpected next character at columns 0-0 [^] in expression [^\d{5}(?:[-\s]\d{4})?$].
Try to add /
before ^
and after the $
sign.
E.g.
ng-pattern="/^\d{5}(?:[-\s]\d{4})?$/"
Hope it helps!
By default angularjs wraps regular expression with ^
and $
symbols. Remove those.
Fragment from code:
var f, g = d.ngPattern || d.pattern;
d.$observe("pattern", function(a) {
C(a) && 0 < a.length && (a = new RegExp("^" + a + "$"));
If you want to put your regex in code, rather in html:
In controller:
function SomeController() {
var vm = this;
vm.regex = /^\d{5}(?:[-\s]\d{4})?$/.source;
}
In html (assuming your controller is aliased as "ctrl"):
ng-pattern="ctrl.regex"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With