我试过的
^((?!root).)*$结果
root无效用户名admin有效用户名rootwee无效用户名(这应该是有效的)root123无效用户名(这应该是有效的)我试着将.从regex中移除,但是它不起作用。
你能帮个忙吗?
发布于 2015-09-10 09:14:31
regex的特性是一个经过调整的贪婪令牌,它不允许整个输入中的某个子字符串。因此,"rootwee“和"root123”是无效的。
您可以使用
/^(?!root$)/请参阅演示
锚定的前瞻性(?!root$)确保整个输入不等于root,但是字符串本身可以包含root。
注意,当使用文本regex声明时,我们不需要匹配整个输入字符串。
下面是一个演示片段:
function formCtrl($scope){
$scope.onSubmit = function(){
alert("form submitted");
}
}<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="formCtrl">
<form name="myForm" ng-submit="onSubmit()">
<input type="text" name="field" ng-model="formCtrl" ng-pattern="/^(?!root$)/" required>
<span ng-show="myForm.field.$error.pattern">Not valid!</span>
<span ng-show="myForm.field.$error.required">This field is required!</span>
<input type="submit" value="submit"/>
</form>
</div>
https://stackoverflow.com/questions/32497629
复制相似问题