Skip to main content
CalcHive

Regex for HTML Tag

Matches HTML opening and self-closing tags with their attributes. Useful for extracting or counting tags in HTML content.

Pattern

/<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*\/?>/g

Pattern Breakdown

TokenExplanation
<Opening angle bracket
([a-zA-Z][a-zA-Z0-9]*)Tag name (starts with letter)
\bWord boundary
[^>]*Any attributes (everything except >)
\/?>Optional self-closing slash and closing bracket

Test Examples

InputMatch?
<div>Match
<img src="test.jpg" />Match
<a href="#">link</a>Match
just textNo match

Common Use Cases

  • HTML parsing and extraction
  • Tag counting
  • Content scraping
  • Template analysis

Variations

<\/?[a-zA-Z][^>]*>

Matches both opening and closing tags

Want to test this pattern with your own data?

Open the Regex Tester