Regex for URL
This regex matches HTTP and HTTPS URLs including optional www prefix, domain name, and optional path, query string, and fragment components. It supports standard URL characters and percent-encoded values. The pattern works well for extracting URLs from plain text and for basic URL validation in forms.
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*) What is the regex pattern for URL?
The regex pattern for URL is https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*) with the gi flags. This regex matches HTTP and HTTPS URLs including optional www prefix, domain name, and optional path, query string, and fragment components. It supports standard URL characters and percent-encoded values. The pattern works well for extracting URLs from plain text and for basic URL validation in forms. This pattern is commonly used for link extraction from text and url validation in forms.
Test Examples
Visit https://example.com/path?q=1 https://example.com/path?q=1 http://www.test.org http://www.test.org not a url Common Uses
- ✓ Link extraction from text
- ✓ URL validation in forms
- ✓ Web scraping
- ✓ Chat message link detection
Variations
With protocol required
https?:\/\/[^\s]+ Simpler, matches any non-whitespace after protocol
Any protocol
[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s]+ Matches ftp://, ssh://, and other protocols
Without protocol
(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*) Matches URLs without http/https prefix
Frequently Asked Questions
Does this regex match URLs without a protocol?
No, this pattern requires http:// or https:// at the start. If you need to match bare domains like example.com, use the 'Without protocol' variation listed above.
Can this regex validate all valid URLs?
It covers the most common URL formats but does not handle every edge case in the URL specification, such as internationalized domain names (IDNs) or unusual port numbers. For strict validation, use your language's built-in URL parser.
Why does this regex not match FTP or other protocols?
It is specifically designed for web URLs (HTTP/HTTPS), which are the most common use case. Use the 'Any protocol' variation if you need to match other schemes like ftp:// or ssh://.