0

I'm trying to get a regular expression to work where the following URLs are accepted:

www.somesite.com
somesite.com
www.somesite.ca
somesite.ca
somesite.cu.sk.ca
www.somsite.cu.sk.ca
somesite.sk.ca
www.somesite.sk.ca

I have the following so far but it allows www.somesite

 ^(www\.)?[a-zA-Z0-9_\-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}.[a-zA-Z]{2})(.[a-zA-z]{2})?$

Query strings, http, https, ftp are not in play here. Thanks!

1
  • why use regex for this? Commented Jun 8, 2015 at 19:23

1 Answer 1

2

You forgot to escape . in the last pattern (.[a-zA-z]{2}) (the dot will match any character):

^(www\.)?[\w-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}.[a-zA-Z]{2})(\.[a-zA-z]{2})?$
                                                         ↑

See DEMO

Also, I replaced your [a-zA-Z0-9_\-] with its equivalent [\w-]

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

1 Comment

Business requirements @Josh Stevens

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.