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

can JavaScript regular expression cause browsers crash. How does urlPath = urlPath.replace(/(.*)+(#)$/i,'$1'); crashed chrome

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

这是表达式。 :

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

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

PS。我试图摆脱要传递给 window.location.href

的 url 字符串末尾的散列

是的,它可以做到 ReDoS 如果你在 #

之后得到一些东西

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

这会起作用

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

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

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

console.log('