const regex = new RegExp('^[1-9]\\d{0,3} ?[a-zA-Z]?(?: ?[/-] ?[1-9]\\d{0,3} ?[a-zA-Z]?)?$', 'gm')
const str = `############# valid ##########
12
12a
12A
12 A
12 a
12 b
12 z
121 b
56/58
56/58a
56-58
56 - 58
56-58a
1234
1234 a
56/58a
18a - 19b
18a-19b
############# not valid ##########
12345
25-ab
12ü
12_
asdfghjklöqwertzuioyxcvbnm
12!"§\$\$%&/()
a2
a2
13àâäèéêë
0
123aaa123,
123 aaa 123
1 a 1
1a 1'
1a1
13àâäèéêë
00 a
a a
00a
0a
12 ab 12`;
// 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