Вот DOMAIN + SUBDOMAIN решение, которое может помочь кому-то еще:
/^([a-zA-Z0-9]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?\.)?([a-zA-Z0-9]{1,2}([-a-zA-Z0-9]{0,252}[a-zA-Z0-9])?)\.([a-zA-Z]{2,63})$/
, который проходит следующие тесты Chai:
const expect = require('chai').expect;
function testDomainValidNamesRegExp(val) {
let names = /^([a-zA-Z0-9]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9])?\.)?([a-zA-Z0-9]([-a-zA-Z0-9]{0,252}[a-zA-Z0-9])?)\.([a-zA-Z]{2,63})$/;
return names.test(val);
}
let validDomainNames = [
"example.com",
"try.direct",
"my-example.com",
"subdomain.example.com",
"example.com",
"example23.com",
"regexp-1222.org",
"read-book.net",
"org.host.org",
"org.host.org",
"velmart.shop-products.md",
"ip2email.terronosp-222.lb",
"stack.com",
"sta-ck.com",
"sta---ck.com",
"9sta--ck.com",
"sta--ck9.com",
"stack99.com",
"99stack.com",
"sta99ck.com",
"sub.do.com",
"ss.sss-ss.ss",
"s.sss-ss.ss",
"s.s-s.ss",
"test.t.te"
];
let invalidDomainNames = [
"example2.com222",
"@example.ru:?",
"example22:89",
"@jefe@dd.ru@22-",
"example.net?1222",
"example.com:8080:",
".example.com:8080:",
"---test.com",
"$dollars$.gb",
"sell-.me",
"open22.the-door@koll.ru",
"mem-.wer().or%:222",
"pop().addjocker.lon",
"regular-l=.heroes?",
" ecmas cript-8.org ",
"example.com::%",
"example:8080",
"example",
"examaple.com:*",
"-test.test.com",
"-test.com",
"dd-.test.com",
"dfgdfg.dfgdf33.e",
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd-.test.com",
"dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd.testttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt.com",
"d-.test.com"
];
describe("Test Domain Valid Names RegExp", () => {
validDomainNames.forEach((val) => {
it(`Text: ${val}`, () => {
expect(testDomainValidNamesRegExp(val)).to.be.true;
});
});
});
describe("Test Domain Invalid Names RegExp", () => {
invalidDomainNames.forEach((val) => {
it(`Text: ${val}`, () => {
expect(testDomainValidNamesRegExp(val)).to.be.false;
});
});
});
Дополнительные тесты приветствуются!