Skip to content

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.

Pattern flags: gi
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)
Test this pattern in the Regex Tester →

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

Match
Visit https://example.com/path?q=1
Matches: https://example.com/path?q=1
Match
http://www.test.org
Matches: http://www.test.org
No Match
not a url

Common Uses

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://.

Related Patterns

Domain Name

^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-z...

Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA...

IPv4 Address

^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3...

Related Reading

Regex Cheat Sheet with Examples for Developers → URL Encoding Special Characters →