JavaScript Regex无限循环的某些模式 [英] JavaScript Regex Infinite Loop on Some Patterns

查看:60
本文介绍了JavaScript Regex无限循环的某些模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript Regex 对象上使用 exec 方法,并且可能进入无限循环,其中 exec 不返回null取决于表达式.

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.

这是我编写的用于说明问题的测试功能.我在Chrome 32中运行它.我在循环外部定义了 Regex match 变量. max / Reached Max 测试可以打破无限循环.

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. The max/Reached Max test is there to break out of the infinite loop.

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

这是我要使用的正则表达式.它提取一个可选的变量名和一个必需的XPath表达式.这将进入无限循环,只有我添加的测试才能停止该循环.它似乎到达输入文本的结尾并挂起.

Here is the regular expression I am trying to use. It extracts an optional variable name and a required XPath expression. 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

如果文本包含换行符 \ n ,它将挂在它前面的字符上.

If the text includes a new-line, \n, it would hang at the character before it.

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

我将不胜感激.韦斯.

推荐答案

模式.* 与第一个匹配项之后的源字符串中的零个字符匹配.它将永远匹配那些零字符.您可以通过首先匹配空字符串来简化该演示.

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.

这篇关于JavaScript Regex无限循环的某些模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆