0

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

这就是表达。:

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

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

附言。我试图摆脱要传递给的 url 字符串末尾的哈希window.location.href

4

1 回答 1

2

是的,如果你以后能得到一些东西,它可以做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]);

于 2018-11-30T13:46:40.133 回答