const regex = /(Source_addresses:(?:.|\n)*?Applications:.*\n)/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(Source_addresses:(?:.|\\n)*?Applications:.*\\n)', 'g')
const str = `Policy name: example-3 (lf-csd) (LINE 29907)
Description: CHANGE00001
Destination_addresses (8):
10.0.0.1/32 ( 10.0.0.1-lk-3r53) ( 3125)
10.0.0.8/32 ( 10.0.0.8-test13) ( 3157)
10.0.0.3/32 ( 10.0.0.3-gdgsd-cd) ( 2806)
10.0.0.9/32 ( 10.0.0.9-fd-fdsb) ( 3123)
Destination_address_sets (0):
Source_addresses:
10.0.0.7/32 ( 10.0.0.7-IR) ( 5989)
Applications: tcp3389
Then: permit
Policy name: example-4 (lfs-csd) (LINE 29907)
Description: CHANGE00002
Destination_addresses (3):
10.0.0.6/32 ( 10.0.0.1-lk-3r53) ( 3125)
10.0.0.2/32 ( 10.0.0.8-test13) ( 3157)
10.0.0.53/32 ( 10.0.0.3-gdgsd-cd) ( 2806)
10.0.0.94/32 ( 10.0.0.9-fd-fdsb) ( 3123)
Destination_address_sets (0):
Source_addresses:
10.0.0.53/32 ( 10.0.0.52-IR) ( 5989)
Applications: tcp53
Then: permit`;
// 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