Javascript 在ng模式中匹配[/,.]的正则表达式
我试图创建一个表达式,使输入字符串不能包含正斜杠、逗号或点Javascript 在ng模式中匹配[/,.]的正则表达式,javascript,angularjs,regex,Javascript,Angularjs,Regex,我试图创建一个表达式,使输入字符串不能包含正斜杠、逗号或点 <form name="myForm"> <div class="col-sm-4"> <input class="form-control" type="text" data-ng-model="model_name" name="modelName" ng-pattern="/^[\/,.]/" required>
<form name="myForm">
<div class="col-sm-4">
<input class="form-control"
type="text"
data-ng-model="model_name"
name="modelName"
ng-pattern="/^[\/,.]/" required>
<div ng-messages="myForm.$submitted && myForm.modelName.$error" role="alert">
<div class="alert alert-danger" ng-message="pattern">Special characters [/ , .] are not allowed.</div>
<div class="alert alert-danger" ng-message="required">This field is required.</div>
</div>
</div>
</form>
不允许使用特殊字符[/,.]。
此字段必填。
正则表达式
/^[\/,.]/
正确匹配方括号内的所有字符,但问题是它不允许我输入正常字符串,如abc或abc_def或任何其他字符串。我对正则表达式没有深入的了解,也不知道如何解决这个问题。请提供帮助。您的正则表达式是错误的,^
必须在[]
内才能匹配除/以外的所有字符,
将它们放在[^/,]
中,在regex中,当在块内使用^
时[]
将匹配除[]
内的任何字符以外的任何字符
在测试正则表达式时,此web应用程序非常有用:您的解决方案可以正常工作。我在方括号后使用了a+,并删除了g。我在这里测试了这个模式[我不知道为什么它对我不起作用。我还将myForm.modelName.$error
更改为myForm.modelName.$valid
ng pattern=“/[^\/,.]+/”
不起作用,所以我创建了$scope.regex=“[^\/,.]+”
并编写了ng pattern=“regex”
成功了。你对此有什么想法吗?对不起,我对angular不太熟悉。可能你需要指定模式将是带有ng pattern=“regex”