JS里的正则匹配会出现无穷解最终导致浏览器卡死
为什么下面的正则会让浏览器卡死呢?
tag_re = '```';
text = "ccccc\nddddddddasdfasdfasdfasdfasdfasdfaddddsdfasahtml\n`";
console.log(text.match(new RegExp('^(.+?|\\n[^\\n])*?[^`]' + tag_re + '[^`]')));
熊猫汽水萌萌哒
10 years, 1 month ago
Answers
应该是 浏览器 JS正则引擎 实现的问题. 实际测一下:
text = "ccccc\nddddddddasdfasdfasdfasdfasdfasdfaddddsdfasahtml\n`";
console.log(text.match(new RegExp('^(.+?|\\n[^\\n])*?[^`]```[^`]')));
IE10中瞬间返回; Chrome38.0.2125.101瞬间返回; FireFox33 中浏览器死掉.
在perl中运行类似的正则的话, 可以在debug中看到:
Intuit: trying to determine minimum start position...
Did not find floating substr "```"...
Match rejected by optimizer
因为找不到 ```, 直接没有过正则就返回了.
镜花水月丶
answered 10 years, 1 month ago