JavaScript正则表达式会导致浏览器崩溃吗?urlPath = urlPath.replace(/(.*)+(#)$/i,'$1');坠毁的铬



我在开发控制台中添加了一个简单的正则表达式,chrome firefox和其他浏览器都疯了。

这就是表达式

urlPath = window.location.href;
urlPath = urlPath.replace(/(.*)+(#)$/i,'$1');

为什么浏览器会崩溃?我没有得到任何线索。任何帮助都将不胜感激。

PS。我试图去掉要传递给window.location.href的url字符串末尾的哈希

是的,如果你在#之后得到一些东西,它可以执行ReDoS

更改为/(.*)(#)$/i

这将工作

console.log('12345678901234567890#12345'.replace(/(.*)(#)$/i, '$1'));
但这会挂起你的浏览器

console.log('12345678901234567890#12345'.replace(/(.*)+(#)$/i, '$1'));

如果您想在#之前匹配所有内容,请使用此^([^#]*)

console.log('https://stackoverflow.com/questions/53558707/can-javascript-regular-expression-cause-browsers-crash-how-does-urlpath-urlpa#123123123'.match(/^([^#]*)/)[0]);

最新更新