regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match a URL

Copy the pattern, paste your text below, and see every http/https link it finds. Below the tester: what each part does and why matching every possible URL is a trap.

Regex pattern

/https?://[^\s/$.?#].[^\s]*/g

This matches an http or https URL starting with a valid host character, e.g. https://example.com/path?q=1. It is a practical matcher for prose, not a full RFC 3986 validator.

/ /

What this pattern matches

This matches the URLs people actually paste: an http or https scheme, a host, and any path or query that follows, stopping at the first whitespace.

How it works, part by part

https?:// — http or https followed by ://.

[^\s/$.?#] — the first host character, which cannot be whitespace or a URL delimiter.

.[^\s]* — the rest of the URL up to the next space.

Why not match everything

A regex that accepts every RFC 3986 URL (mailto:, ftp:, IDN hosts, bare domains) becomes unreadable and matches junk. This pragmatic version catches the web links in real text. To also catch bare www. links, prepend (?:https?://|www\.).

Use it in your code

Python
import re
text = "Visit https://example.com/path?q=1 or http://sub.domain.co.uk. Bare www.foo.com is skipped, and (no scheme) is skipped too."
pattern = r'''https?://[^\s/$.?#].[^\s]*'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Visit https://example.com/path?q=1 or http://sub.domain.co.uk. Bare www.foo.com is skipped, and (no scheme) is skipped too.";
const re = new RegExp(String.raw`https?://[^\s/$.?#].[^\s]*`, "g");
console.log(text.match(re));

FAQ

Does it match a URL with a query string?

Yes. Everything after the host up to the next whitespace is captured, including ?q=1&page=2.

Will it match bare domains like example.com?

No. It requires an http/https scheme. Add www\. as an alternative if you need bare www links.

How do I stop it capturing a trailing period or comma?

In prose, add a trim step or replace [^\s]* with [^\s]*[^\s.,)] so it drops trailing punctuation.