const regex = /(?:^\s*
(?<name>[\w-]+\.[\w-.]*?)\.\s*
(?<ttl>\d+)\s*
(?<class>\w*)\s*
(?<type>\w*)
(?(?<=SOA)
\s*(?<rdata>
(?<mname>\S*)\.\s*
(?<rname>\S*)\.\s*
(?<serial>\d*)\s*
(?<refresh>\d*)\s*
(?<retry>\d*)\s*
(?<expire>\d*)\s*
(?<minimum>\d*)\s*
)\s*$
|
\s*(?<rdata>\S*)\s*$
)
)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?:^\\s*
(?<name>[\\w-]+\\.[\\w-.]*?)\\.\\s*
(?<ttl>\\d+)\\s*
(?<class>\\w*)\\s*
(?<type>\\w*)
(?(?<=SOA)
\\s*(?<rdata>
(?<mname>\\S*)\\.\\s*
(?<rname>\\S*)\\.\\s*
(?<serial>\\d*)\\s*
(?<refresh>\\d*)\\s*
(?<retry>\\d*)\\s*
(?<expire>\\d*)\\s*
(?<minimum>\\d*)\\s*
)\\s*$
|
\\s*(?<rdata>\\S*)\\s*$
)
)', 'gm')
const str = `agamerica-qa.commloanservices.com.
====================================
agamerica-qa.commloanservices.com. 3600 IN SOA dns001.pnc.com. hostmaster.pnc.com. 543792585 1200 180 1209600 600
agamerica-qa.commloanservices.com. 86400 IN NS dns002.pnc.com.
agamerica-qa.commloanservices.com. 86400 IN NS dns001.pnc.com.
agamerica-qa.commloanservices.com. 86400 IN NS dns000.pnc.com.
bin.agamerica-qa.commloanservices.com. 30 IN CNAME gf0.agamerica-qa.commloanservices.com.
gf0.agamerica-qa.commloanservices.com. 3600 IN A 161.150.124.249
agamerica-qa.commloanservices.com. 3600 IN SOA dns001.pnc.com. hostmaster.pnc.com. 543792585 1200 180 1209600 600
agamerica.commloanservices.com.
====================================
agamerica.commloanservices.com. 3600 IN SOA dns001.pnc.com. hostmaster.pnc.com. 543792587 1200 180 1209600 600
agamerica.commloanservices.com. 86400 IN NS dns002.pnc.com.
agamerica.commloanservices.com. 86400 IN NS dns001.pnc.com.
agamerica.commloanservices.com. 86400 IN NS dns000.pnc.com.
agamerica.commloanservices.com. 3600 IN A 161.150.133.216
bin.agamerica.commloanservices.com. 30 IN CNAME gf2.agamerica.commloanservices.com.
gf1.agamerica.commloanservices.com. 3600 IN A 161.150.116.249
gf2.agamerica.commloanservices.com. 3600 IN A 161.150.40.249
agamerica.commloanservices.com. 3600 IN SOA dns001.pnc.com. hostmaster.pnc.com. 543792587 1200 180 1209600 600
`;
// 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