URL (With And Without Port Number) Regular Expressions

A regular expression to match all valid URLs with port numbers, hashes(#), and parameters (&).

/^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/

If you’re looking for a regex that matches a URL with a port number:

/^((ht|f)tps?:\/\/)?[\w-]+(\.[\w-]+)+:\d{1,5}\/?$/

URL Examples:

  • https://regexpattern.com/
  • https://regexpattern.com/#/a=1&b=2
  • https://regexpattern.com/:8080
  • ftps://regexpattern.com/

See Also: