Check whether a string looks like a valid URL using a pragmatic regular-expression pattern for HTTP/HTTPS hosts, domain names, IPv4 addresses, optional ports, paths, queries, and fragments.
When to use it
- Quick sanity check before storing user-submitted links
- Debug configuration values in CI logs
- Teach URL structure without issuing network requests
Limitations
Pattern validation is not the same as reachability—no DNS or HTTP request is made. Internationalized domain names (IDN) in Unicode form may not match. Does not validate certificate trust or malicious destinations.
Example
https://example.com/path?q=1#top validates as a well-formed HTTP(S) URL with path, query, and fragment. http://127.0.0.1:8080/health also matches the IPv4-plus-port pattern. Strings such as notaurl, ftp://files.example (wrong scheme for this pattern), or bare example.com without a scheme fail the check—remember this is shape validation, not a live fetch.