[英]JavaScript Regex Infinite Loop on Some Patterns
I am trying to use the exec
method on the JavaScript Regex
object and can get into an infinite loop where exec
does not return a null depending on the expression. 我正在尝试在JavaScript
Regex
对象上使用exec
方法,并且可能进入无限循环,其中exec
不会根据表达式返回null。
Here is a test function I wrote to illustrate the problem. 这是我编写的用于说明问题的测试功能。 I ran it in Chrome 32. I am defining the
Regex
and match
variables outside the loop. 我在Chrome 32中运行它。我正在定义正则
Regex
并在循环外match
变量。 The max
/ Reached Max
test is there to break out of the infinite loop. max
/已Reached Max
测试可以突破无限循环。
function textExec(reg, text, max) {
max = max || 10
var match = null;
while (match = reg.exec(text)) {
console.log(match);
console.log(match.length + " " + match.index + "," + reg.lastIndex);
if (--max < 0 || match.index == reg.lastIndex) {
console.log('Reached Max');
break;
}
}
}
Here is a simple test that runs as expected. 这是一个按预期运行的简单测试。
textExec(/(o[a-z])/g, "body=//soap:Body");
["od", "od", index: 1, input: "body=//soap:Body"]
2 1,3
["oa", "oa", index: 8, input: "body=//soap:Body"]
2 8,10
["od", "od", index: 13, input: "body=//soap:Body"]
2 13,15
Here is the regular expression I am trying to use. 这是我要使用的正则表达式。 It extracts an optional variable name and a required XPath expression.
它提取一个可选的变量名和一个必需的XPath表达式。 This will go into an infinite loop that is only stopped by the test I added.
这将进入无限循环,只有通过我添加的测试才能停止。 It appears to get to the end of the input text and hang.
它似乎到达输入文本的结尾并挂起。
textExec(/(([a-zA-Z0-9_-]*)=)?(.*)/g, "body=//soap:Body");
["body=//soap:Body", "body=", "body", "//soap:Body", index: 0, input: "body=//soap:Body"]
4 0,16
["", undefined, undefined, "", index: 16, input: "body=//soap:Body"]
4 16,16
Reached Max
Here is the same test simplified. 这是简化的相同测试。 It still sends it into an infinite loop.
它仍然将其发送到无限循环中。
textExec(/.*/g, "body=//soap:Body");
["body=//soap:Body", index: 0, input: "body=//soap:Body"]
1 0,16
["", index: 16, input: "body=//soap:Body"]
1 16,16
Reached Max
If the text includes a new-line, \\n
, it would hang at the character before it. 如果文本包含换行符
\\n
,它将挂在它前面的字符处。
textExec(/.*/g, "//soap:Envelope\n//soap:Body");
["//soap:Envelope", index: 0, input: "//soap:Envelope?//soap:Body"]
1 0,15
["", index: 15, input: "//soap:Envelope\n//soap:Body"]
1 15,15
Reached Max
I would appreciate any help. 我将不胜感激任何帮助。 Wes.
韦斯。
The pattern .*
matches the zero characters in the source string that come after the first match. 模式
.*
匹配源字符串中第一次匹配后的零个字符。 It will keep on matching those zero characters forever. 它将永远匹配那些零字符。 You could simplify a demonstration of that by matching against the empty string in the first place.
您可以通过首先匹配空字符串来简化该演示。
What you could do is quit when the match position stops changing. 当比赛位置停止改变时,您可以做的是退出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.