How-to

How to Extract URLs from Text with Regex

Use a permissive URL pattern with the g flag and String.matchAll. A practical regex accepts http/https, optional www, and common URL characters.

A practical URL-extraction regex

The fully RFC-3986-compliant URL regex is massive. For most extraction use cases — finding links in a blog post, a support ticket, or an email body — a pragmatic pattern is enough.

Extract http/https URLs

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

Input

See https://example.com/docs?id=42 and http://b.test/

Result

Matches: https://example.com/docs?id=42, http://b.test/

Using it in JavaScript

Call String.matchAll (or use a global regex with String.match) to get every URL, not just the first. The result is an iterator of match arrays; map to match[0] for just the URL strings.

Trim trailing punctuation

URLs embedded in prose often end up with a period, comma, or parenthesis stuck on the end — 'visit https://example.com.' The regex above WILL include the period. A cleanup pass with .replace(/[.,)\]]+$/, '') on each match fixes this at the cost of potentially trimming legitimate trailing characters in rare cases.

When you need stricter matching

If you're validating user-submitted URLs (not extracting from prose), use the anchored URL-validation pattern and cross-check against URL parsing logic. The extraction pattern intentionally errs toward over-matching; validation should err toward under-matching.

Related patterns

All reference guidesOpen the RegexPro tester →