0

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);

2 Answers 2

1

Try this:

/^(?=www\.)?[A-Za-z0-9_-]+\.+[A-Za-z0-9.\/%&=\?_:;-]+$/

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the simple answer - just ading the ? is what I needed.
0

This seems to work when tested on http://www.regextester.com/

/^(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.