How can I allow users to enter both subdomain and domain names without the http:// prefix using a regex in javascript. I need to allow: domainname.com or www.domainname.com or www.domainname.co.uk. I have this at the moment which expects www. :
/^(?=www\.)[A-Za-z0-9_-]+\.+[A-Za-z0-9.\/%&=\?_:;-]+$/ix.test(value);
Try this:
Marking the www group with a ? makes it a zero-or-one match, which is what you want as I understand it.
Tested with http://www.regular-expressions.info/javascriptexample.html