简体   繁体   English

Angular ng-pattern崩溃浏览器

[英]Angular ng-pattern Crashes Browser

We have a regular expression that we use for e-mail. 我们有一个用于电子邮件的正则表达式。 Our application is inheriting the regular expression, so it may not be an option to switch it... Anyway, the same set of steps seem to crash the javascript in the browser. 我们的应用程序继承了正则表达式,所以它可能不是一个选项来切换它...无论如何,同样的一组步骤似乎会使浏览器中的javascript崩溃。 I've been able to reproduce in IE and Chrome, but not Firefox. 我已经能够在IE和Chrome中重现,但不能在Firefox中重现。 Here is the code: 这是代码:

var mod = angular.module("myApp", []);

mod.controller("MainCtrl", function ($scope) {
    //Pattern that blows up the browser during ng-pattern
    $scope.emailPattern = /^(?!.*\.{2})([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+([\.][a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*)@((([\-]?[a-zA-Z0-9]){2,}[\.])*(([a-zA-Z0-9][\-]?){1,})+).(([\.]([a-zA-Z0-9][\-]?){2,}([a-zA-Z0-9])*)+)$/;


});

HTML: HTML:

<div ng-app="myApp" ng-controller="MainCtrl">
    <form name="emailForm" novalidate>
        <input type="text" ng-model="user.email" name="email" maxlength="80" required ng-pattern="emailPattern">
    </form>
    <br>
    {{user.email}}
</div>

Fiddle here . 在这里小提琴 Here are the basic steps that will cause the browser to crash: 以下是导致浏览器崩溃的基本步骤:

  1. Type a bunch of alpha characters into the text box until it is full (other inputs may work, but this is what I've used). 在文本框中键入一堆字母字符,直到它已满(其他输入可能有效,但这是我使用过的)。
  2. Backspace 2 characters. 退格2个字符。
  3. Hit the Home key to return to the front of the input. Home键返回输入的前面。
  4. Type the characters: a@ 输入字符: a @

This will crash IE and Chrome consistently. 这将使IE和Chrome一直崩溃。 Has anybody encountered this bug before? 以前有人遇到过这个bug吗? Any known workarounds? 任何已知的解决方法?

I think the issue is with the regex expression itself. 我认为问题在于正则表达式本身。

If you went to any regex validator site (like http://regexpal.com/ ), using the original expression, the same problem would occur there (with the browser freezing). 如果您使用原始表达式访问任何正则表达式验证程序站点(如http://regexpal.com/ ),则会出现同样的问题(浏览器冻结)。


Change: 更改:

$scope.emailPattern = /^(?!.*\.{2})([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+([\.][a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*)@((([\-]?[a-zA-Z0-9]){2,}[\.])*(([a-zA-Z0-9][\-]?){1,})+).(([\.]([a-zA-Z0-9][\-]?){2,}([a-zA-Z0-9])*)+)$/;

To: 至:

$scope.emailPattern = /^(?!.*\.{2})([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+([\.][a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*)@((([\-]?[a-zA-Z0-9]){2,}[\.])*(([a-zA-Z0-9][\-]?))+).(([\.]([a-zA-Z0-9][\-]?){2,}([a-zA-Z0-9])*)+)$/;

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

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