const regex = /(?=(\d{2}\/\d{2}\/\d{4}))(?=([^\n]+\n)+[^\n]*BNA\sCNTRS[^=\n]*\n)([^\n]+\n)+/gs;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?=(\\d{2}\\\/\\d{2}\\\/\\d{4}))(?=([^\\n]+\\n)+[^\\n]*BNA\\sCNTRS[^=\\n]*\\n)([^\\n]+\\n)+', 'gs')
const str = `*044*05/02/2013*14:24*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
OPERATION OK
*044*05/02/2013*14:24*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
*044*05/02/2013*14:24*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
OPERATION OK
[0r(1)2[000p[040qe1w3h162*054*05/04/2013*14:27*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
*055*05/04/2013*14:27*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
OPERATION OK
[020t*057*05/04/2013*14:27*
[05p
BNA CNTRS
LAST CLEARED : 00/00/00 00:00
COUNT
ENCASHED 141
[0r(1)2[000p[040qe1w3h162*065*05/05/2013*14:30*
*1234*1*(Iw(1*0, M-00, R-10011002100310041
A/C
OPERATION OK
*071*05/06/2013*14:31*
*1234*1*(Iw(1*1, M-00, R-10011002100310041
A/C
CUSTOMER CANCEL
[020t*076*05/06/2013*14:32*
[05p
BNA CNTRS
LAST CLEARED : 05/04/13 14:28
COUNT
ENCASHED 11`;
// 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