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.
How the pattern works
https?://— the scheme, the part you actually want to allow (javascript:anddata:URLs are excluded — a security win).[^\s/$.?#]— first host character cannot be whitespace, slash, dot, question mark or hash (killshttps://.junk).[^\s]*— the rest runs until whitespace, which is how links terminate in prose and logs.- Stricter host shape (labels + TLD):
https?://([a-z0-9-]+\.)+[a-z]{2,}(:[0-9]+)?(/[^\s]*)?— trades some real-world coverage for structure. - Linkifying in the browser? Check the anchor protocol in code too — regex alone cannot guarantee a resolvable host.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
read https://regextester.site/ga4-regex-cheatsheet/ now | Match | full link extracted | Try → |
http://localhost:3000/app?q=1 | Match | port + query ok | Try → |
visit example.com for more | No match | scheme required | Try → |
https:// | Match | minimal — see pitfalls | Try → |
javascript:alert(1) | No match | only http(s) allowed | Try → |
ftp://files.example.com/a.zip | No match | other schemes excluded | Try → |
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
- Scheme-less
example.com(very common in the wild) is deliberately rejected — decide per data source whether to allow(https?://)?. - Trailing punctuation:
(see https://x.co.)swallows the closing paren — trim[.,)]+$from matches in prose. - Anchored vs search: use
$when validating a whole field; drop anchors when extracting from sentences.
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.
Related patterns
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.