当执行我正则表达式和不匹配输入的浏览器选项卡stucks [英] Browser tab stucks when my RegEx is executed and do not match the input

查看:171
本文介绍了当执行我正则表达式和不匹配输入的浏览器选项卡stucks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是下一个。我创建了具有验证输入字段,这是有效的数据:

The problem is next. I created an input field which has validation and this is valid data:


  1. 1-12,14,16,22,25-35,41,49,55-90

  2. 1230年至1992年,2001至99年,9931

  3. 1-2

  4. 13

  5. 1,3,4,5,6,10

  6. 所有

基本上,这些数字(范围,以逗号分隔的范围,用逗号分隔的数字,逗号后的空格,逗号后没有空格,一句话:'所有')的任意组合

Basically, any combination of those numbers (ranges, comma-separated ranges, comma-separated number, spaces after commas, no spaces after commas, word: 'all')

我的正则表达式: /^(([0-9]{0,4},?\\s{0,})+([0-9]{1,4}-[0-9]{1,4}){0,},?\\s{0,})+$|^(all)$|^([0-9]{1,4}-[0-9]{1,4}){0,},?\\s{0,}$/

My RegEx: /^(([0-9]{0,4},?\s{0,})+([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,})+$|^(all)$|^([0-9]{1,4}-[0-9]{1,4}){0,},?\s{0,}$/

这作品几乎罚款,只有1主要问题。

It works almost fine, there is only 1 major problem.

当我开始打字,有的逗号分隔的数字后,我补充一点,就是不合法,喜欢的字符:'ABC' - 而这时我的浏览器选项卡中的 stucks

When I start typing, and after some comma-separated numbers I add something that is not valid, like characters: 'abc' - and at this moment my browser tab stucks.

试试这个作为一个输入: 1-12,14,16,19,20-29,是

Try this as an input: 1-12, 14, 16, 19, 20-29, was

小提琴测试: http://jsfiddle.net/c9ahfhqy/

是否有正确的正则表达式应该怎么样子的?

Is there any suggestions on how the proper RegEx should look like?

code:

    $("input[type='text']").blur(function() { //when you lose focus on input
      doneTyping();
    });

    function doneTyping() {
      if(!$('#inputToCheck').val().match(/^(([0-9]{0,4},?\s{0,})+([0-9]{1,4}\-[0-9]{1,4}){0,},?\s{0,})+$|^(all)$|^([0-9]{1,4}\-[0-9]{1,4}){0,},?\s{0,}$/)) {
        console.log("Not Valid");
      } else {
        console.log("Valid");
      }
    }

真的AP preciate帮助。

Really appreciate help.

推荐答案

@derp <一个href=\"http://stackoverflow.com/questions/25433413/browser-tab-stucks-when-my-regex-is-executed-and-do-not-match-the-input/25434078#comment39680057_25433413\">said,您正则表达式有灾难性回溯

As @derp said, your regex has a problem of Catastrophic Backtracking.

下面的人似乎像你想要的工作,并没有冻结浏览器:

The following one seems to work like you want, and doesn't freeze the browser:

/^(?:all|\d{1,4}(?:-\d{1,4})?(?:,\s*\d{1,4}(?:-\d{1,4})?)*)$/

演示

使用使用RegexBuddy的调试器来测试 1-12,14,16,19,20-29,是

Using RegexBuddy's debugger to test 1-12, 14, 16, 19, 20-29, was,


  • 您正则表达式试图百万的步骤,用这种错误之后失败:

  • You regex fails after attempting 1000000 steps, with this error:

您定期的前pression太复杂,继续调试。结果
  您打算与使用正则表达式引擎可能无法在处理它
  所有和崩溃。结果
  查一查灾难性回溯中的帮助文件,了解如何避免
  这种情况。

Your regular expression is too complex to continue debugging.
The regex engine you plan to use it with may not be able to handle it at all and crash.
Look up "catastrophic backtracking" in the help file to learn how to avoid this situation.

正则表达式后96以上步骤失败,并没有错误。

  • The regex above fails after 96 steps, with no error.

    这篇关于当执行我正则表达式和不匹配输入的浏览器选项卡stucks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆