const regex = /(?m)^.*?Exception.*(?:\R+^\s*at .*)+/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?m)^.*?Exception.*(?:\\R+^\\s*at .*)+', 'g')
const str = ` INFO [main] (AutoMain.java:133) - querying data 1
DEBUG [main] (AutoMain.java:142) - data 1 count: 23180
INFO [main] (AutoMain.java:151) - querying data 2
ERROR [main] (AutoMain.java:607) - Failure in auto
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at com.myCompany.client.ClientIOFactory\$1.<init>(ClientIOFactory.java:17)
at com.myCompany.client.ClientIOFactory.lambda\$clientIOFactoryFromSocket\$0(ClientIOFactory.java:15)
at com.myCompany.client.queryData(Client.java:83)
at com.myCompany.client.queryData(Client.java:91)
at com.myCompany.queryOptData(InstantAutomaton.java:153)
at com.myCompany.AutoMain.main(InstantAutomaton.java:426)
`;
// 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