onetwopunch
onetwopunch

Reputation: 3349

Host name url regex in JavaScript

I know there are many RegExs out there for URLs etc, but I'm really stumped as to why this one wont work. I'm trying to make sure a user's entry is a proper web site, allowing for either www or not.

var pattern = 
/http(s?):\/\/                      (http:// or https://)
(?:[a-zA-Z]+\.){0,1}                (0 or 1 instance of www)
(?:[a-zA-Z0-9][a-zA-Z0-9-]+){1}     (1 instance of a domain name)
(?:\.[a-zA-Z]{2,6})+/;              (1 or more instance of .com or .co.uk)

It works for the following testcases using pattern.test()

However, even though I explicitly tell the regex it must match the .com or .co.uk, etc. it allows me to write such nonsense as:

I've been wracking my brain trying different combos but I can understand why even though I explicitly say there must be one or more domain extension, that portion doesn't seem to matter?

Cheers

Upvotes: 1

Views: 2564

Answers (1)

Ry-
Ry-

Reputation: 225144

If you’re trying to validate a string, you need to anchor it to the beginning and end of the string:

var pattern = 
/^http(s?):\/\/                      (http:// or https://)
(?:[a-zA-Z]+\.){0,1}                (0 or 1 instance of www)
(?:[a-zA-Z0-9][a-zA-Z0-9-]+){1}     (1 instance of a domain name)
(?:\.[a-zA-Z]{2,6})+$/;              (1 or more instance of .com or .co.uk)

For www.stackoverflow, the first group isn’t matching (because you said it’s optional), then the second group is matching www, and the third group is matching .stacko.

Tips:

  • {1} is not necessary
  • {0,1} is equivalent to ?
  • Since you’re not using any captures, (s?) could just be s?

Upvotes: 4

Related Questions