Regex tester / pattern library
Regex: url (http/https)
/https?:\/\/[\w.-]+(:\d+)?(\/[^\s?#]*)?(\?[^\s#]*)?(#\S*)?/g Test it live How it works
Scheme, host, optional port, then optional path, query, and fragment — each segment stopping at whitespace. The g flag extracts every URL from a block of text rather than validating a single one.
Honest caveats
Extraction-grade, not validation-grade: it will happily grab a trailing punctuation mark glued to a URL in prose. For strict validation, parse with the URL constructor instead of a regex.
Matches
https://tarhun.dev/regexhttp://localhost:8080/api?x=1https://sub.example.co.uk#top
Doesn't match
ftp://server/filewww.example.comhttps://