Regex for URL Validation — Test URL Patterns
Quick Answer: A practical URL regex: ^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$. For production validation, prefer the URL constructor (new URL(input)) which handles edge cases regex cannot.
FAQ
Should I use regex or the URL constructor for validation?
Use new URL(input) in a try/catch for programmatic validation. It handles edge cases, IDN domains, and IPv6 addresses. Use regex only for quick format checks in forms.
Does this match URLs without http/https?
The pattern above requires http:// or https://. To match bare domains like example.com, remove the protocol part -- but this increases false positives significantly.