I need a regular expression which will check the domain name including characters like a-z A-Z 0-9 -
Also does any domain name having underscore _ character?
Please help me.
I guess this is what youre searching for:
^[a-zA-Z0-9]+([a-zA-Z0-9-.]+)?.(com|org|net|mil|edu|de|COM|ORG|NET|MIL|EDU|DE)$
i modifier to your regex to prevent duplication!This regular expression would work on any number of subdomains and all top level domains (the world is not just murka):
/^ (?: [a-z0-9] (?:[a-z0-9\-]* [a-z0-9])? \. )* #Subdomains
[a-z0-9] (?:[a-z0-9\-]* [a-z0-9])? #Domain
\. [a-z]{2,6} $ #Top-level domain
/ix
Domain names cannot have underscores. Only [a-zA-Z0-9], as well as hyphens if they are not at the beginning or the end of a subdomain name.
filter_var()withFILTER_VALIDATE_URL?