const regex = new RegExp('^(?P<bucket>[^\\/]+)\\/(?:(?P<prefix>.+)\\/)?AWSLogs\\/(?P<aws_account_id>[\\d]{12})\\/elasticloadbalancing\\/(?P<region>[\\w\\d\\-]+)\\/(?P<year>\\d{4})\\/(?P<month>\\d{2})\\/(?P<day>\\d{2})\\/(?P=aws_account_id)_elasticloadbalancing_(?P=region)_(?P<load_balancer_id>[\\w\\.\\-\\d]+)_(?P<end_time>\\d{8}T\\d{4}Z)_(?P<ip_address>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))_(?P<random_string>.+)\\.log\\.gz$', 'gm')
const str = `log-bucket/AWSLogs/113901497002/elasticloadbalancing/us-east-1/2020/04/26/113901497002_elasticloadbalancing_us-east-1_app.us-east-lt-alb.5ac1c221b7cf2245_20200426T0225Z_34.233.232.149_nhinzchd.log.gz
log-bucket/web-lt/AWSLogs/113901497002/elasticloadbalancing/us-east-1/2020/04/26/113901497002_elasticloadbalancing_us-east-1_app.us-east-lt-alb.5ac1c221b7cf2245_20200426T0225Z_34.233.232.149_nhinzchd.log.gz
`;
// 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