Here is my input:
start
@var=somevar1
@var=somevar2
end
I am using this regex
start(?:\s*\n*(?:@var=(.*)\s*)*)\s*\n*end
Its should give the output as
somevar1
somevar2
but its giving just somevar2.
Is there any way to get all occurrence of the capturing group?
One option is to use a positive lookbehind with an inifite quantifier to assert start followed by a newline at the left.
See the support for lookbehinds.
(?<=^start\n[^]*@var=)\S+(?=[^]*\nend$)
const regex = /(?<=^start\n[^]*@var=)\S+(?=[^]*\nend$)/gm;
const str = `start
@var=somevar1
@var=somevar2
end`;
let m;
console.log(str.match(regex));
If there can only be formats of @var=somevar preceding and following instead of other content:
(?<=^start\n\s*(?:@var=\S+\s+)*@var=)\S+(?=(?:\s*@var=\S+)*\s*\nend$)
See another regex demo