Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-pattern gives "Lexer Error"?

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})?$].
like image 625
ds011591 Avatar asked Jul 02 '15 15:07

ds011591


3 Answers

Try to add / before ^ and after the $ sign.

E.g.

ng-pattern="/^\d{5}(?:[-\s]\d{4})?$/"

Hope it helps!

like image 100
Alberto I.N.J. Avatar answered Oct 15 '22 11:10

Alberto I.N.J.


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 + "$"));
like image 26
Engineer Avatar answered Oct 15 '22 12:10

Engineer


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"
like image 39
Dror Weiss Avatar answered Oct 15 '22 12:10

Dror Weiss