I am trying to create a regex for first time for the URL having following conditions:
- Starts with or without the www
- Do not starts with http:// or https://
- Allowed special characters are: hyphen (-) and dot (.)
- Ends with a TLD like: .com, .io, .co.in, etc.
Some of the examples for reference:
Valid:
xyz.com
xyz-px.com
www.xyz.com
abc.xyz.px.io
www.abc.xyz.px.io
Invalid:
xyz-.com
xy_pm.com
http://xyz.io
https://xyz.io
http://www.pm.xyz
[email protected]
xyz.io$#
www.xy
xyz.io-
I have created a regex
/(^(?!https|http)?:\/\/(www\.?)[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,})/gi
But it is not working as desired.

^[^\W_]+(?:[-.][^\W_]+)*(?:\.[a-z]{2,})+$regex101.com/r/E08Ce3/1