简体   繁体   English

正则表达式匹配ng-pattern中的[/,。]

[英]regular expresstion to match [/ , .] in ng-pattern

I am trying to create a expression so that input string must not contain forward slash, comma or a dot. 我正在尝试创建一个表达式,以便输入字符串不得包含正斜杠,逗号或点。

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

The regular expression /^[\\/,.]/ properly matches all the characters inside the square brackets but the problem is that it is not allowing me to input normal strings such as abc or abc_def or any other strings. 正则表达式/^[\\/,.]/正确匹配方括号内的所有字符,但是问题在于它不允许我输入普通字符串,例如abc或abc_def或任何其他字符串。 I don't have deep knowledge of regex and I am not getting any idea how to solve this problem. 我对正则表达式没有很深的了解,我也不知道如何解决此问题。 Please help. 请帮忙。

Your regex is wrong, ^ must be inside [] in order to match all characters except /., put them in a [^/.,] , in regex, when using ^ inside a block [] will match anything but what is inside the [] . 您的regex是错误的, ^必须位于[]内才能匹配/.,以外的所有字符/.,将它们放在regex中的[^/.,]中,当在块中使用^时, []会匹配除内部内容以外的任何字符[]

在此处输入图片说明

When testing regex, this web it useful: http://www.regexpal.com/ 在测试正则表达式时,此网站非常有用: http : //www.regexpal.com/

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

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