简体   繁体   English

Angular.js ng-pattern奇怪的行为

[英]Angular.js ng-pattern strange behaviour

I have been working on a form that accepts Twitter parameters such as # and @ to populate a Twitter feed. 我一直在研究一个接受Twitter参数的表单,例如#和@来填充Twitter提要。

With Angular.js I had planned to use the built in ng-pattern directive to validate the input before saving, however the validation is acting extremely strangely. 使用Angular.js,我曾计划使用内置的ng-pattern指令在保存之前验证输入,但验证操作非常奇怪。 It marks a "valid" string as invalid on every 2nd character of the input while typing. 在键入时,它会在输入的每个第二个字符上将“有效”字符串标记为无效。

Its quite hard to explain the exact behaviour so heres a Plunker . 它很难解释确切的行为所以继承人是一个普通人

For completeness I will add my input field with the strange ng-pattern here: 为了完整性,我将在这里添加带有奇怪ng-pattern输入字段:

<input type="text" ng-pattern="/(^|\\s)@(\\w+)|(^|\\s)#(\\w+)/g" ng-model="foo" name="foo"/>

It's because of the global matching with the g option, it works if you take it out. 这是因为全局匹配g选项,如果你把它拿出来就有效。

Calling test or exec multiple times involves state : 多次调用testexec 涉及状态

As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match. 与exec(或与其结合)一样,在同一个全局正则表达式实例上多次调用的测试将超过上一个匹配。

Basically it's trying to move on to another match, but can't find one: 基本上它正试图继续进行另一场比赛,但找不到一场:

a = /@(\w+)$/g;
> /@(\w+)$/g
a.exec("@test")
> ["@test", "test"]
a.exec("@test")
> null

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

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