1

I have regex expression (1) which matches the URL specified below. However I have problem creating a regex for input URL (see not matching). Any ideas how to do this on regex where user can input a URL with combination of http, https or www or even without these three but will still match the URL?

(1)
^(http|https)://www.example.com

(1) Matches
http://www.example.com
https://www.example.com

Not Matching
http://example.com
https://example.com

Should Match
http://www.example.com
https://www.example.com
http://example.com
https://example.com
example.com
2
  • what about www.example.com. Commented Jun 11, 2015 at 5:29
  • yeah i think nu11p01n73R nailed it. THanks Commented Jun 11, 2015 at 5:32

2 Answers 2

2

Make the https:// and www optional by using ? quantifiers.

^(?:(?:http|https)://)?(?:www\.)?example\.com

Regex Demo

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

1 Comment

THanks nu really appreciate it!
2
^(?:(?:http|https):\/\/)?(?:www\\.)?example\\.com$

Just appended a $ and a \ before .com to nu11p01n73R solution in case you wanted your match to end at .com

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.