HTML TagRegex — Pattern, Examples & Code
<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>/gWhat it matches / doesn't match
| Matches ✅ | Does not match ❌ |
|---|---|
●<div> | ●<123invalid> |
●<a href="url"> | ●not a tag |
●</p> | ●< spaced > |
How it works
Matches HTML opening tags, closing tags, and self-closing tags. The tag name must start with a letter followed by alphanumeric characters. Attributes (if any) are captured as a raw string. For production HTML parsing, always use a dedicated parser like the browser's DOM or a library — regex is not reliable for nested or malformed HTML.
Code Usage
JavaScript
const regex = /<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>$/g; "<div class=\"foo\">".match(regex); // ["<div class=\"foo\">"]
Python
import re
pattern = re.compile(r'<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>')
pattern.findall("<div class='foo'>") # ['div']Go
import "regexp"
r := regexp.MustCompile(`</?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>`)
r.FindStringSubmatch("<div>") // ["<div>", "div"]Need a custom regex for a different use case?
Generate a custom regex with AI →FAQ
What does the HTML Tag regex pattern match?
Matches HTML opening tags, closing tags, and self-closing tags. The tag name must start with a letter followed by alphanumeric characters. Attributes (if any) are captured as a raw string. For production HTML parsing, always use a dedicated parser like the browser's DOM or a library — regex is not reliable for nested or malformed HTML.
How do I use the HTML Tag regex in JavaScript?
const regex = /<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>$/g; "<div class=\"foo\">".match(regex); // ["<div class=\"foo\">"]
Can I use the HTML Tag regex in Python?
Yes. import re pattern = re.compile(r'<\/?([a-zA-Z][a-zA-Z0-9]*)(?:\s[^>]*)?>') pattern.findall("<div class='foo'>") # ['div']
