URL Pattern
Match HTTP and HTTPS URLs with optional port, path, query string, and fragment.
What Is This?
This regex pattern matches HTTP and HTTPS URLs including optional components like port numbers, URL path, query string parameters, and fragment identifiers. It is designed for extracting or validating web URLs in text content.
How to Use
The Pattern
The pattern matches strings starting with http:// or https:// followed by a domain, optional port (:3000), optional path with forward slashes, optional query string (?key=value), and optional fragment (#section). Percent-encoded characters (%20) are supported in paths.
/https?:\/\/[\w.-]+(:\d+)?(\/[\w./%-]*)?(\?[\w&=.-]*)?(#[\w-]*)?/i
Examples
Basic URLs
Matches: https://example.com http://example.com/page https://example.com:8080/path Does not match: ftp://example.com example.com https://
URLs with query and fragment
Matches: https://example.com/page?q=search&lang=en https://example.com/page#section1 https://example.com/path%20with%20spaces Does not match: https:// http:///path https://example .com
Related Entries
More from this reference:
Email Address Validation
Validate email addresses with a standard pattern covering most common formats.
Phone Number (US/International)
Match US and international phone numbers with optional country code and separators.
Date (YYYY-MM-DD)
Match dates in ISO 8601 YYYY-MM-DD format with basic month and day validation.
Credit Card Number (Generic)
Match generic credit card numbers in grouped or continuous 16-digit format.
UUID v4
Match UUID v4 identifiers with the standard 8-4-4-4-12 hex format and version check.
Base64 Encoded String
Match standard Base64 encoded strings with optional padding.
Time (HH:MM 24-hour)
Match times in 24-hour HH:MM format with valid hour and minute ranges.
Frequently Asked Questions
Does this pattern match all valid URLs?
No. This pattern matches common HTTP/HTTPS URLs but does not cover FTP, mailto, file, or other protocol URLs. For comprehensive URL matching, consider using a URL parsing library.
Should I validate or sanitize URLs?
Always sanitize URLs in addition to validating them. Malicious URLs can pass regex validation. Use URL parsing libraries and allowlisting for production security.