const regex = /\[(?P<severity>\w+)\]\s*(?P<remote>[^\s:]+):(?P<port>\d+)\s*-\s*(?P<id>\d+)\s*"(?P<type>[\w]+)\s*(?P<class>\w+)\s*(?P<name>[^\s]+)\s*(?P<proto>\w+)\s*(?P<size>\d+)\s*(?P<do>\w+)\s*(?P<bufsize>\d+)"\s*(?P<rcode>\w+)\s*(?P<rflags>[^\s]+)\s+(?P<rsize>\d+)\s*(?P<duration>[^\s]+)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\[(?P<severity>\\w+)\\]\\s*(?P<remote>[^\\s:]+):(?P<port>\\d+)\\s*-\\s*(?P<id>\\d+)\\s*"(?P<type>[\\w]+)\\s*(?P<class>\\w+)\\s*(?P<name>[^\\s]+)\\s*(?P<proto>\\w+)\\s*(?P<size>\\d+)\\s*(?P<do>\\w+)\\s*(?P<bufsize>\\d+)"\\s*(?P<rcode>\\w+)\\s*(?P<rflags>[^\\s]+)\\s+(?P<rsize>\\d+)\\s*(?P<duration>[^\\s]+)', 'gm')
const str = `[INFO] 172.20.164.61:52054 - 29029 "AAAA IN private-link-api.coralogix.in. udp 47 false 512" NOERROR qr,aa,rd,ra 163 0.000061739s
[INFO] 172.20.164.61:52054 - 15579 "A IN private-link-api.coralogix.in.ap-south-1.compute.internal. udp 75 false 512" NXDOMAIN qr,aa,rd,ra 190 0.000081757s
[INFO] 172.20.164.61:52054 - 15579 "A IN private-link-api.coralogix.in.ap-south-1.compute.internal. udp 75 false 512" NXDOMAIN aa,rd,ra 190 0.000081757s
[INFO] 172.20.160.191:44146 - 254 \\"A IN secretsmanager.ap-south-1.amazonaws.com. udp 57 false 512\\" NOERROR qr,aa,rd,ra 222 0.000054345s\\n","time":"2023-02-13T12:14:26.928909225Z`;
// 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