Regex Library
Regex Library

13 patterns

URL Validation Regex Patterns

URL validation is trickier than it looks - protocols, subdomains, ports, query strings, and fragments all need to be handled. These patterns cover the most common URL validation scenarios from simple HTTP/HTTPS checks to full domain extraction.

Common Use Cases

Link validation in formsURL extraction from textDomain validationQuery string parsing

All URL Validation Patterns

HTTP/HTTPS URL

Validates full HTTP and HTTPS URLs.

^https?:\/\/[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$

URL with Parameters

URL with query strings (GET parameters).

^https?:\/\/[^\s]+\?[^\s]+$

Domain Name

Validates a domain name (without protocol).

^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,}$

URL Extraction

Extracts all URLs from a text.

https?:\/\/[^\s<>"]+

Root URL Only

Validates base URL without path or parameters.

^https?:\/\/[A-Za-z0-9.-]+\.[A-Za-z]{2,}$

URL with Anchor

HTTP(S) URL with optional anchor/fragment.

^https?:\/\/[^\s#]+(?:#[^\s]*)?$

Relative URL

Relative path with optional query and hash.

^\/[^\s?#]*(?:\?[\s#]*)?(?:#[^\s]*)?$

Domain Name

Validates domain names (e.g., example.com, sub.domain.co.uk)

^(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.(?:[A-Za-z]{2,}|[A-Za-z]{2,}\.[A-Za-z]{2,})$

Spotify URI

Validates Spotify resource URIs for tracks, albums, artists, playlists

^spotify:(track|album|artist|playlist|user):[a-zA-Z0-9]+$

URL Slug

Validates a URL-friendly slug (lowercase letters, numbers, hyphens - no leading/trailing hyphens).

^[a-z0-9]+(?:-[a-z0-9]+)*$

YouTube Video URL

Matches any standard YouTube URL (watch, embed, shorts, youtu.be) and captures the 11-char video ID.

^https?:\/\/(?:www\.|m\.)?(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/|v\/)|youtu\.be\/)([A-Za-z0-9_-]{11})(?:[?&].*)?$

Vimeo Video URL

Matches a Vimeo video URL (regular or embed) and captures the numeric ID.

^https?:\/\/(?:www\.|player\.)?vimeo\.com\/(?:video\/)?(\d+)(?:[?&/].*)?$

Versioned API Path

Matches REST API paths in the /api/v1/, /api/v2.1/ style.

^\/api\/v\d+(?:\.\d+)?\/[A-Za-z0-9._~/-]+$

Frequently Asked Questions

How do I validate a URL in JavaScript?

For most cases: const isValid = /^https?:\/\/[\w.-]+/.test(url). For strict validation, consider the URL constructor: try { new URL(url); return true; } catch { return false; }

Should I allow FTP URLs?

Only if your application specifically needs it. For web links, restrict to http:// and https://

How do I extract all URLs from a string?

Use the "URL Extraction" pattern with the global flag: string.match(/https?:\/\/[^\s<>"]+/g)

Looking for patterns in other categories?

Browse all 209 patterns