Advertisement

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.

The pattern
</?[a-zA-Z][^<>]*>
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
<p>Hello</p>Matchmatches both <p> and </p>Try →
<br/>Matchself-closing tagTry →
<div class="card">x</div>Matchattributes ride alongTry →
<input type=text>Matchvoid element, no closing tagTry →
<2 fast>No matchtag names start with a letterTry →
a < bNo matchcomparison, 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

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

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.

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.