Javascript 如何包括qutoes+;ng正则表达式中的双引号?
我在android studio中开发了phonegap应用程序 我有一个输入字段,必须允许abc字符、引号和双引号。Javascript 如何包括qutoes+;ng正则表达式中的双引号?,javascript,android,angularjs,regex,Javascript,Android,Angularjs,Regex,我在android studio中开发了phonegap应用程序 我有一个输入字段,必须允许abc字符、引号和双引号。 想要的html代码: <input placeholder="last name" type="text" ng-model="currentUser.LastName" ng-pattern='/^([A-Za-z\"\' ]){1,45}$/' required
想要的html代码:
<input placeholder="last name" type="text" ng-model="currentUser.LastName"
ng-pattern='/^([A-Za-z\"\' ]){1,45}$/'
required
class="form-control"/>
或
但我也找不到包含引号和双引号的方法
我试过:
范围:
$scope.regex=/^([A-Za-z\"\' ]){1,45}$/;
在html页面中:
<input placeholder="last name" type="text" ng-model="currentUser.LastName" name="userLastName1"
ng-pattern='{{regex}}'
required
class="form-control"/>
它也不起作用(我的编辑器是android studio,也许其他编辑器更好?)
有没有办法在一个正则表达式中同时包含引号和双引号?您可以对这些实体使用十六进制值,
\x22
用于“
和\x27
用于”
:
$scope.regex= "/^[A-Za-z\x22\x27 ]{1,45}$/";
不需要外圆括号
你可以测试一下
或者,下面是一个代码片段,显示它在ng模式下工作
属性:
函数formCtrl($scope){
$scope.onSubmit=function(){
警报(“提交的表格”);
}
}
无效!
此字段必填!
您不需要插入scope变量。您应该能够将其作为$scope.regex=/^([A-Za-z\“\']”){1,45}$/
然后在模板中使用它作为ng pattern='regex'
我刚刚编辑了它,这是一个有问题的错误,真正的代码是正确的,但它不起作用。它不起作用是什么意思?很高兴它为您起作用。请考虑接受这个答案。顺便说一句,ng pattern=“/^[A-Za-z'\x22]{1,45}$/”
也应该有效(无需“混淆”单个撇号)。
<input placeholder="last name" type="text" ng-model="currentUser.LastName" name="userLastName1"
ng-pattern='{{regex}}'
required
class="form-control"/>
$scope.regex= "/^[A-Za-z\x22\x27 ]{1,45}$/";