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

痴心易碎 提交于 2019-12-05 05:07:00
Oriol

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.

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