VSO
VSO

Reputation: 12646

RegEx in Ng-Pattern Behaving Differently when in String, as Opposed to Var

When I use a RegEx as a string to only allow digits, it works fine, like so:

 <input ng-model="txtInputValueOne" 
        ng-pattern = "/^\d+$/">

When I use it with a variable, it does NOT work correctly ("working correctly" = allowing only digits into the input model result):

 <input ng-model="txtInputValueTwo" 
        ng-pattern = "intPattern">


$scope.regExString = "\d+"
$scope.intPattern =  new RegExp($scope.regExString);

What's the problem? PLUNK

Upvotes: 0

Views: 76

Answers (1)

thegio
thegio

Reputation: 1243

You need to escape the backslash

$scope.regExString = "^\\d+$"

Upvotes: 1

Related Questions