elefcode

Regex pattern

URL Regex — free online tester

Extract every http(s) URL from a block of text — captures protocol, domain, path, query string, and fragment.

The pattern

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

How it works

Real-world URLs vary wildly: query strings, hash fragments, ports, sub-paths, percent-encoded characters. This regex matches the protocol prefix (http:// or https://) followed by any non-whitespace run, which covers virtually every URL you'll find in user-generated text, log files, or scraped HTML.

When to use it

  • Auto-linking plain-text comments or chat messages
  • Extracting URLs from log files and crash reports
  • Cleaning up scraped HTML
  • Stripping links from input for sanitisation

Tip

If you only want canonical URLs (no trailing punctuation in sentences), post-process matches with a trim of `.,;:!?` from the right side.

Try it on this input

Visit https://elefcode.com for free dev tools, or http://example.org/path/?q=hello&n=3#top
Docs: https://docs.example.com/api/v2#section-1
Old protocol: ftp://files.example.com — not matched on purpose
Just text: not a URL, no link here.

What this pattern doesn't catch

  • Doesn't match ftp://, file://, mailto:, or other schemes (intentional — broaden the prefix if you need them)
  • Punctuation at the end of a sentence (".", "?", ",") becomes part of the URL — strip it after matching
  • Won't catch bare-domain URLs like "example.com" without a protocol

More regex patterns