简体   繁体   English

与字符串相反,Ng模式中的RegEx在字符串中时的行为不同

[英]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: 当我使用RegEx作为仅允许数字的字符串时,它可以正常工作,如下所示:

 <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 朋克

您需要转义反斜杠

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM