[\s\S]+
将贪婪地匹配任何字符序列,包括换行符,但您只希望搜索到
Fail
或
Done
遇到。因为
Some Text
行总是只有一行,通过(在命令之后)匹配
单一的
[\s\S]
(换行符),后跟一行字符,然后是另一行字符
[s s] +
(换行),后跟
失败
.
const input = `
Starting.. path/to/first.command
Some Text..
Done
Starting.. other/path/to/second.command
Some Other Text..
Done
Starting.. other/path/to/third.command
Some Text..
Fail
Starting.. other/path/to/forth.command
Some Other Text..
Fail
`;
const re = /Starting\.\. (.+\.command)[\s\S].+[\s\S] +Fail/g;
let match;
while (match = re.exec(input)) {
console.log(match[1]);
}
如果使用(更新的,不受支持的)lookbehind,则更简单:
const input = `
Starting.. path/to/first.command
Some Text..
Done
Starting.. other/path/to/second.command
Some Other Text..
Done
Starting.. other/path/to/third.command
Some Text..
Fail
Starting.. other/path/to/forth.command
Some Other Text..
Fail
`;
const re = /(?<=Starting\.\. +).+\.command(?=[\s\S].+[\s\S] +Fail)/g;
console.log(input.match(re));