Back to all patterns
URL Extraction
Extraction
Extract URLs starting with http or https from a larger block of text.
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gHow it works
This regex is useful for finding all hyperlinks within a raw text string. It looks for the protocol (http or https), followed by the domain name, and optionally captures the path and query parameters.
Test Cases
Should Match
- https://www.google.com
- http://example.com/path?query=1
Should NOT Match
- www.google.com
- ftp://example.com
Quick Summary
Extracts http and https URLs from free-form text. Uses the global flag to find all occurrences in a string. Captures the full URL including path, query string, and fragment.
Key Takeaways
Key Takeaways
- Matches http:// and https:// URLs only, not ftp://, mailto:, or protocol-relative //
- The global flag (g) returns all matches in a string, not just the first
- Captures path, query string (?key=value), and fragment (#section) when present
- Does not validate that the URL is reachable, only that it is syntactically a URL
Use Cases
When to use it
- Extracting links from scraped HTML or plain text
- Building a link-checker that scans documents for URLs
- Parsing log files to find referenced external resources
Watch out
Common Mistakes
- Forgetting the global flag when you need all URLs in a string
- Using this on HTML, a proper HTML parser is more reliable for href attributes
- Assuming the extracted URL is safe to visit, always sanitize before use
FAQ
URL Extraction, Frequently Asked
Why doesn't this match ftp:// URLs?
The pattern anchors on https?:// which only covers http and https. Add ftp to the alternation (https?|ftp) if needed.
How do I extract just the domain from a URL?
After extracting the full URL, apply a second regex like /^https?:\/\/([^\/]+)/ to capture the hostname.