Browser tab stucks when my RegEx is executed and do not match the input

心不动则不痛 提交于 2019-12-22 05:07:59

问题


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-2099, 9931
  3. 1-2
  4. 13
  5. 1,3,4,5,6,10
  6. all

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

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,}$/

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

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.

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

Fiddle to test: 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");
      }
    }

Really appreciate help.


回答1:


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})?)*)$/

Demo

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

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

    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.

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



来源:https://stackoverflow.com/questions/25433413/browser-tab-stucks-when-my-regex-is-executed-and-do-not-match-the-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!