regex pattern · ready to copy
Regex for matching URLs
Capture http(s) URLs from free text — comments, logs, markdown.
intermediate
javascript / pcre / python4 use cases
The pattern
https?://[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=]*
Test cases
| Input | Result |
|---|---|
| https://voiddo.com/tools | ✓matches |
| http://localhost:3000/api/v1 | ✓matches |
| https://docs.example.io/path?q=1#frag | ✓matches |
| ftp://nope | ✗rejects |
| just-text | ✗rejects |
| google.com (no scheme) | ✗rejects |
Edge cases & caveats
Greedy on trailing punctuation — strip trailing `.`, `,`, `)` after match. For domain-only matching (no scheme), drop the `https?://` prefix and leave `\w.-]+\.\w{2,}`.
Common use cases
- link extraction from comments
- broken-link audit
- social post URL counting
- scraping for sitemap seed
Try variations against your data
regexlab is a free in-browser tester with side-by-side match highlighting, group inspector, and named-capture export to JS/Python/PCRE.
Open regexlab
Related
iban · credit card number · us zip code · jwt