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
| Token | Explanation |
|---|---|
| ^ | 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
| Input | Match? |
|---|---|
| https://example.com | Match |
| http://sub.domain.co.uk/path?q=1 | Match |
| https://example.com/path/to/page | Match |
| ftp://example.com | No match |
| not a url | No 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
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester