ds011591
ds011591

Reputation: 494

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})?$].

Upvotes: 8

Views: 9329

Answers (4)

Akansha Srivastava
Akansha Srivastava

Reputation: 231

If anyone is facing problem similar to : Lexer Error: Unexpected next character at columns 7-7 [#] in expression [consts.#Contact]

With:

Controller:

app.controller('XXController',
    function($rootScope, $scope, $q, XXService, configuration, constants) {

    $scope.consts = constants.constants;
    //...
});

constants_en.json:

{

 "#Contact":"Contact Details",
...

}

HTML:

<label for="contact">{{ consts.#Contact }} </label>

Solution : Use following in HTML:

<label for="customer">{{ consts['#Contact'] }}</label>

Upvotes: 0

dror
dror

Reputation: 3936

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"

Upvotes: 2

Engineer
Engineer

Reputation: 48793

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 + "$"));

Upvotes: 5

Alberto I.N.J.
Alberto I.N.J.

Reputation: 1853

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

E.g.

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

Hope it helps!

Upvotes: 28

Related Questions