Advertisement

Pattern library / URLs & Marketing

Practical URL Regex (http / https)

RFC 3986 full validation is thousands of characters of character-class soup. In practice you need to find links in text and reject obvious junk — which needs about 40. This pattern requires a scheme and a plausible host, accepts ports and paths, and stays valid in RE2 engines.

The pattern
https?://[^\s/$.?#][^\s]*
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
read https://regextester.site/ga4-regex-cheatsheet/ nowMatchfull link extractedTry →
http://localhost:3000/app?q=1Matchport + query okTry →
visit example.com for moreNo matchscheme requiredTry →
https://Matchminimal — see pitfallsTry →
javascript:alert(1)No matchonly http(s) allowedTry →
ftp://files.example.com/a.zipNo matchother schemes excludedTry →

Where to use it

JavaScript (find all links)

const urls = text.match(/https?:\/\/[^\s/$.?#][^\s]*/g) || [];

Python (validate one)

import re
URL = r'^https?://[^\s/$.?#][^\s]*$'
bool(re.fullmatch(URL, 'https://regextester.site/'))

BigQuery / GA4 (RE2)

REGEXP_EXTRACT(page_location, r'^(https?://[^/?#]+)')  # origin only

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

Why not use the full RFC 3986 URL regex?

It is unmaintainable, slow on long inputs, and still misses real-world URLs. The practical version matches what appears in logs and text while keeping junk out.

How do I match URLs without a scheme (example.com)?

Make the scheme optional: (https?://)?([^\s/$.?#][^\s]*) — but decide first: protocol-relative and bare domains create false positives in prose.

Is this pattern safe for RE2 / GA4?

Yes — negated classes, literals and quantifiers only. The stricter host variant is RE2-compatible as well.

Extract Domain from URL — Regex Capture Group · UTM Parameter Regex — Match & Extract utm_* Tags · Remove Query Parameters with Regex (Replace Example)

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.