Scenario
I'm using ng-pattern
in a project where I need to dynamically generate regular expressions via string concatenation meaning the end result is var regexp = "regexp"
rather than var regexp = /regexp/
.
The Problem
I know that this code works:
//JavaScript
$scope.regex = /[^0-9\|]+/;
//HTML
<input type="text" name="first" ng-model="a" ng-pattern="regex">
But I need to be able to use a string like so:
//JavaScript
$scope.regex = "[^0-9\|]+";
//HTML
<input type="text" name="first" ng-model="a" ng-pattern="{{regex}}">
The Question
How can I use my dynamically generated string regular expression with ng-pattern
?
Plunker
I've prepped a plunker demonstrating three different uses of ng-pattern
in which only the literal regular expression works. I'd be so grateful for any help. Thanks!