const regex = /(?<=^label )\S+(?!.*(?:\n(?!label).*)*?\n[ \t]*\[end_timeline\])/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=^label )\\S+(?!.*(?:\\n(?!label).*)*?\\n[ \\t]*\\[end_timeline\\])', 'gm')
const str = `label Colorcode (Object)
Dialog
Speaker: "Text"
Speaker 2: "[i]Text[/i]! [pause={pause.medium}] more text."
do function_name("parameter", {parameter})
# comment, there are no inline-comments
[end_timeline]
label Maroon (Guitar)
Speaker: "Text"
[end_timeline]
label Pink (Chest)
Speaker: "Text"
label Königsblau (Wardrobe)
Speaker: "Text"
Speaker: "Text"
Speaker: "Text"
[end_timeline]
label Azur (Sorcerers Hat)
Speaker: "Text"
# [end_timeline]
label Jade (Paintings)
Speaker: "Text"
label Gras (Ship in a Bottle)
Speaker: "Text"
Speaker: "Text"
[end_timeline]
label Goldgelb (Golden Apple)
Speaker: "Text"
[end_timeline]
label Himmelblau (Helmet)
Speaker: "Text"
Speaker: "Text"
Speaker: "Text"
Speaker: "Text"`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions