JSFiddle - React, Tailwind, and code Playground

JavaScript

var regex = /^(?!www\.|\d{1,3}\.)[a-z0-9-]+?\.[a-z0-9-]{3,}\.[a-z0-9-]+?(\.[a-z0-9-]+?)*?$/gi;

var hostname = "example.com";
console.log(hostname.match(regex));

hostname = "www.example.com";
console.log(hostname.match(regex));

hostname = "test.example.com";
console.log(hostname.match(regex));

hostname = "www.example.com";
console.log(hostname.match(regex));

hostname = "example.co.in";
console.log(hostname.match(regex));

hostname = "www.example.co.in";
console.log(hostname.match(regex));

hostname = "1test.example.co.in";
console.log(hostname.match(regex));

hostname = "187.162.10.12";
console.log(hostname.match(regex));