0
/^[_a-z0-9]+(\.[_a-z0-9]+)*[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,7})$/

That's the pattern. It says "ok" when it's correct, and "not ok" when it's incorrect. So it says "ok" on www.google.com, but "not ok" when I type http://www.google.com

What I'd like is this pattern to allow http:// too, but it should never be a requirement.

1

2 Answers 2

1

You can use the following regular expression, which I lifted off borrowed from the documentation of the Perl module URI on CPAN (escaping of slashes mine).

/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/

It will give you all the different parts of the URI in capture groups.

Those parts are:

  • scheme (http:)
  • authority (not applicable here)
  • path (www.google.com)
  • query (q=querystring)
  • fragment (#anker)

See https://regex101.com/r/vS5qO1/1 to try it out.

Also note that this will parse all types of URIs, not only http(s). So stuff like ftp://[email protected] will also work.

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

Comments

1

If you're looking to only allow http/https URL schemes (when the scheme is provided), the following modification to your regular expression will do the trick:

/^(http:\/\/|https:\/\/)?[_a-z0-9]+(\.[_a-z0-9]+)*[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,7})$/

You can use my answer as an example of how to add the url scheme group to your existing regex, but credit @simbabque, as he has a much more complete answer.

2 Comments

Thanks for the edit @simbabque . I didn't want to make any gender assumptions when all your profile pic shows is the top of your head, lol!
Yes, I agree. I usually use they and them.

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.