const regex = /^(02|03|04|06|08|09|072|074|076|077|078|079|050|051|052|053|054|055|056|058|059)((?:(?![1,0]{1}))\d{7})$/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('^(02|03|04|06|08|09|072|074|076|077|078|079|050|051|052|053|054|055|056|058|059)((?:(?![1,0]{1}))\\d{7})$', 'gm')
const str = `Valid:
022345677
032345677
042345677
062345677
082345677
092345677
0722345677
022345677
0742345677
0762345677
0742345677
0762345677
0772345677
0782345677
0792345677
0502345677
0512345677
0522345677
0532345677
0542345677
0552345677
0562345677
0582345677
0592345677
NotValid :
021345677
030345677
142345677
362345677
282345677
492345677
5722345677
622345677
7742345677
8762345677
9742345677
`;
// 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