const regex = /^(?<nr_classe>(\d{4,}))([\x20\t\-]{1,}|[a-zA-Z]|$)/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(?<nr_classe>(\\d{4,}))([\\x20\\t\\-]{1,}|[a-zA-Z]|$)', 'gm')
const str = `250156304 - 6 ANO A INTEGRAL ANUA
250156445 - 7 ANO A INTEGRAL ANUA
250794211 - 2ª SERIE A INTEGRAL ANUA
250794484 - 2ª SERIE B INTEGRAL ANUA
250795069 - 3ª SERIE A INTEGRAL ANUA
250795291 - 3ª SERIE B INTEGRAL ANUA
250797255 - 6 ANO A INTEGRAL ANUA
250797644 - 7 ANO A INTEGRAL ANUA
250797800 - 7 ANO B INTEGRAL ANUA
250798147 - 8 ANO A INTEGRAL ANUA
250798493 - 9 ANO A INTEGRAL ANUA
250798725 - 9 ANO B INTEGRAL ANUA
251327540 - 8 ANO A TARDE ANUA
251327755 - 8 ANO B TARDE ANUA
251328951 - 7 ANO A TARDE ANUA
251329173 - 7 ANO B TARDE ANUA
251329173 -7 ANO B TARDE ANUA
251331013 - 6 ANO A TARDE ANUA
252381546 - 6074 - INFORMATICA PARA INTERNET - 1ª SERIE NT MANHA ANUA`;
// 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