Pattern library / Dev & Logs
HTML Tag Regex
The find-any-tag pattern is one line: </?[a-zA-Z][^<>]*>. It matches <p>, </p>, <br/> and every attribute soup in between. The useful skill is knowing which scoping variant to reach for — and exactly where regex yields to a real parser.
How the pattern works
</?— the optional slash covers opening and closing;[a-zA-Z]forces a letter so<2and bare< >never start a match.[^<>]*— everything up to the first>. That class (not.*) is why attribute soup cannot run the match away.- Scoped to one tag:
</?div(\s|>|/)— matches<div>and</div>but not<dividend>. Append\bafter the name for glued markup. - Capture attributes with a second pass over the tag text:
([a-zA-Z-]+)="([^"]*)"(or the single-quote variant) pairs name and value.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
<p>Hello</p> | Match | matches both <p> and </p> | Try → |
<br/> | Match | self-closing tag | Try → |
<div class="card">x</div> | Match | attributes ride along | Try → |
<input type=text> | Match | void element, no closing tag | Try → |
<2 fast> | No match | tag names start with a letter | Try → |
a < b | No match | comparison, not a tag (no name after <) | Try → |
Where to use it
JavaScript
const tags = html.match(/<\/?[a-zA-Z][^<>]*>/g) || []; // every tag const div = html.match(/<\/?div(\s|>|\/)/); // scoped to div
Python (tags + attributes)
import re TAG = r'</?[a-zA-Z][^<>]*>' tags = re.findall(TAG, '<p>Hi</p>') # ['<p>', '</p>'] attrs = dict(re.findall(r'([a-zA-Z-]+)="([^"]*)"', '<div class="card">'))
Bash (grep, file audit)
grep -oE '</?[a-zA-Z][^<>]*>' page.html | head
Common mistakes
.*>instead of[^<>]*spans from the first < to the last > on the line — the classic greedy bug. The class cannot cross a tag boundary by construction.- Regex is not an HTML parser: tags inside script strings, comments (
<!-- x -->) and malformed markup fool every pattern. For extraction that must not break, use a DOM parser. - HTML entities in text (
<) are invisible to tag matching but corrupt downstream text extraction — unescape in code after stripping tags.
FAQ
How do I match a specific HTML tag?
Anchor the name: </?div(\s|>|/) matches div tags only, open or close, and rejects <dividend>. Add \b after the name when markup can glue tags together.
Why does .* > match too much?
The greedy dot spans from the first < to the last > on the line. [^<>]* stops at the first > by construction — use it, or lazy .*? when a character class cannot apply.
Can regex extract HTML reliably?
For well-formed snippets and log filtering, yes — [^<>]* keeps matches bounded. For arbitrary documents (nested quotes, script blocks, comments), use a real parser: regex is a scalpel, HTML is a tree.
Related patterns
Quoted String Regex — Match "..." and '...' · URL Regex — Match http/https Links (Practical) · Log Level Regex — Match INFO / WARN / ERROR Lines
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.