简体   繁体   English

ng-pattern不应与提供的模式匹配

[英]ng-pattern should not match supplied pattern

In ng-pattern we have the option of specifying that the field should match a specific pattern. ng-pattern我们可以选择指定字段应该与特定模式匹配。

How can we specify that it should NOT match the specified pattern? 我们如何指定它不应该与指定的模式匹配?

Example, 例,

<input type="text" ng-pattern="/[*|\":<>[\]{}`()';@&$]/" />

Here, I DONT want the field to match the pattern. 在这里,我不希望该字段与模式匹配。 On the contrary, I want to show error if the pattern matches. 相反,如果模式匹配,我想显示错误。

您可以使用负面外观( (?!pattern) )来否定正则表达式:

ng-pattern="/(?![*|\":<>[\]{}`()';@&$])/"

In order to check if a string does not have some substring, you can use a string-start-anchored lookahead: 为了检查字符串是否没有某个子字符串,您可以使用字符串开始锚定的前瞻:

/^(?!.*[*|\x22:<>[\]{}`()';@&$])/

See demo 演示

In AngularJS, the pattern does not have to match the entire string if a RegExp object is passed (the one with delimiters), so this regex suits the current purpose well (just check if a condition is true or false). 在AngularJS中,如果传递一个RegExp对象(具有分隔符的对象),则该模式不必匹配整个字符串,因此该正则表达式很好地适合当前目的(只需检查条件是真还是假)。

Note that the commonly used tempered greedy token solution ^(?:(?![*|\\x22:<>\\[\\]{}`()';@&$]).)*$ (see demo ) can be used, too, but it is less efficient since each position in the input string is checked. 注意常用的淬火贪婪令牌解决方案^(?:(?![*|\\x22:<>\\[\\]{}`()';@&$]).)*$ (见演示 )可以是也使用过,但由于输入字符串中的每个位置都被检查,因此效率较低。

Also, it is convenient to use \\x22 to match a double quote in the ng-pattern value. 此外,使用\\x22来匹配ng-pattern值中的双引号也很方便。

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

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