Skip to main content
CalcHive

Regex for URL

Matches HTTP and HTTPS URLs with a domain, optional path, and query string.

Pattern

/^https?://[\w.-]+(?:\.[a-zA-Z]{2,})(?:/[^\s]*)?$/

Pattern Breakdown

TokenExplanation
^Start of string
https?://Protocol (http:// or https://)
[\w.-]+Domain name characters
(?:\.[a-zA-Z]{2,})TLD (dot followed by 2+ letters)
(?:/[^\s]*)?Optional path and query string (no whitespace)
$End of string

Test Examples

InputMatch?
https://example.comMatch
http://sub.domain.co.uk/path?q=1Match
https://example.com/path/to/pageMatch
ftp://example.comNo match
not a urlNo match

Common Use Cases

  • Link validation
  • URL extraction from text
  • Input sanitization
  • Web scraping

Variations

^(https?|ftp)://[^\s]+$

Includes FTP protocol

https?://[^\s]+

Non-anchored for finding URLs in text

Want to test this pattern with your own data?

Open the Regex Tester