URL Regex Example

Validate common HTTP/HTTPS URLs including optional ports, paths, and query fragments.

This URL regex example checks for common HTTP and HTTPS URLs used in APIs, webhooks, and config fields. It requires an explicit protocol, a host name, and allows optional port and path/query/hash segments. The pattern is useful when you need lightweight format validation before making requests or saving settings. It is not a full RFC URL parser, but it handles most developer-facing cases such as endpoint inputs and callback URLs. For security-sensitive flows, pair regex validation with URL parsing and allowlist checks to prevent malformed or unsafe targets. Use this page as a starting point, then test variations in the Regex Tester to fit your exact constraints, including local policy decisions about ports, internal hostnames, or required path structure.

Pattern

/^https?:\/\/[A-Za-z0-9.-]+(?:\:[0-9]{2,5})?(?:\/[\w\-.~:/?#[\]@!$&'()*+,;=%]*)?$/i

Matches

https://api.example.com/v1/users?id=42
http://localhost.dev:8080/status
https://docs.example.org/guide#section-2

Rejects

ftp://example.com/file
example.com/no-protocol
https://

FAQ

Does this regex support non-HTTP protocols?

No. It is intentionally scoped to HTTP/HTTPS style URLs.

Is regex enough for SSRF protection?

No. Use server-side URL parsing, DNS checks, and network allowlists for security controls.

Can I require HTTPS only?

Yes. Replace the protocol prefix with a strict https:// requirement.

Related: Regex Tester & Generator, Text Diff Checker, JSON vs YAML guide.

Browse all patterns in the regex examples hub.

More regex examples

Email Regex Example - Validate common email-like strings with a practical pattern suitable for frontend form checks.

Phone Number Regex Example - Match normalized phone numbers with optional plus prefix and 7 to 15 digits.

Password Regex Example - Require uppercase, lowercase, number, symbol, and a minimum length for strong-password policies.

IPv4 Regex Example - Match IPv4 addresses with strict 0-255 octet validation for each of four segments.