在父匹配中多次匹配捕获组

Matching capture group multiple times within parent match

假设我有这样一个字符串:

hello blah(1) ndnddnnn div1 - this ;sdhfdkl;a div2 - should i<;oisdjn div3 - be div4 - captured ois;s;urbb !blah aksdhflakjsfadlfjkbafa;km blah(2) ndnddnnn div35 - also ;sdhfdkl;a div24 - these i<;oisdjn div0 - need div94 - capture ois;s;urbb !blah world

我正在尝试提取 blah!blah 之间的特定信息。这是我想要的信息的示例:

这是我正在尝试使用的正则表达式:

/blah\((\d)\)(([\s\S]*?div([\d]) - (\w+) )+)[\s\S]*?(?!\!blah)/g

这是我对我定义的正则表达式字符串的理解:

Here is it in action

出于某种原因,我没有得到我想要的匹配项,如您在上面的 link 中所见。我究竟做错了什么?我上面所做的一些假设是否不正确?感谢您的帮助

这个样本怎么样?对于这种情况,我使用了 regexObj.exec(str)

示例脚本:

var str = "hello blah(1) ndnddnnn div1 - this ;sdhfdkl;a div2 - should i<;oisdjn div3 - be div4 - captured ois;s;urbb !blah aksdhflakjsfadlfjkbafa;km blah(2) ndnddnnn div35 - also ;sdhfdkl;a div24 - these i<;oisdjn div0 - need div94 - capture ois;s;urbb !blah world";
var re  = /\((\d)\)|div(\d.?)\s-\s(\w.*?)\s/g;
var ar = [];
while ((res=re.exec(str)) !== null) {
  if (res[1]) {
    var temp = res[1];
  } else {
    ar.push([temp, res[2], res[3]]);
  }
};
console.log(ar);

结果:

[
    ["1", "1", "this"],
    ["1", "2", "should"],
    ["1", "3", "be"],
    ["1", "4", "captured"],
    ["2", "35", "also"],
    ["2", "24", "these"],
    ["2", "0", "need"],
    ["2", "94", "capture"]
]
  • \((\d)\)用于检索().
  • 括起来的数字
  • div(\d.?)\s-\s(\w.*?)\s 用于从 div#1 - this.
  • 中检索“1”和 "this"

jsfiddle.net

regex101.com

如果我误解了你的问题,我很抱歉。