/^[a-z0-9](-?[a-z0-9])*$/i
const regex = /^[a-z]+(?!-*(-)\1{2,})[a-z0-9-]*[a-z]+$/gmi;
const str = `login
lo-gin
Log23gin
log2-3gin
L--login
L--lo--gin
L---login
---login
login-
login--`;
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}`);
});
}