const regex = /(?<hora>[0-9]{8})\s(?<mix>\d{3})(?<resp>[I|O])/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<hora>[0-9]{8})\\s(?<mix>\\d{3})(?<resp>[I|O])', 'g')
const str = ` 09523744 865O F010@@@@Y1905A46100000002 00000000151100157600STD20001
865O 030202040005 1029 003047100000000012602
865O 00000000000000000000000000000000000000000000517712002534
865O 4898 `;
// 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