gpt4 book ai didi

javascript - 为什么正则表达式需要很长时间才能评估某个值?

转载 作者:行者123 更新时间:2023-11-28 19:45:23 25 4
gpt4 key购买 nike

下面是我的正则表达式:

(https?:\/\/)([a-zA-Z]{2,6}\.)*((?!.*[|!{}[\]^"*;]).)+(\.*)([a-zA-Z0-9\.\-\/\:\?&=_%#]+)+([&|?])+$

它是通过否定前瞻来验证 URL,以允许来自其他语言的字符。

这就是我在 http://regex101.com/#javascript 进行测试时发生的情况:

对于-

http://server.com/path?id=1111111 - NO MATCH

http://server.com/path?id=11111111 - TIMEOUT Your expression took too long to evaluate.

http://server.com/path?id=111111111111111111111& - MATCH

观察结果:

当查询参数的值增加到超过一定长度时,就会超时。

但对于匹配的 URL,参数值的长度并不重要。

为什么超时超过一定长度?我需要修改正则表达式的哪一部分?

注意:RegEx 要求 URL 以 ?& 结尾

提前致谢。

编辑:

What I need is, a regex to validate all standard (for e.g. www.xyz.com or someip:port followed by path parameters and/or query parameters, etc) URLs. It should support characters from other languages as well. With an additional validation to mandate the URL to end with ? or &.

最佳答案

(…+)+([a-zA-Z0-9\.\-\/\:\?&=_%#]+)+导致 catastrophic backtracking 。删除其中一项优点应该会有所帮助。

关于javascript - 为什么正则表达式需要很长时间才能评估某个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24363616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com