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[^>]*\/?>/gPattern Breakdown
| Token | Explanation |
|---|---|
| < | Opening angle bracket |
| ([a-zA-Z][a-zA-Z0-9]*) | Tag name (starts with letter) |
| \b | Word boundary |
| [^>]* | Any attributes (everything except >) |
| \/?> | Optional self-closing slash and closing bracket |
Test Examples
| Input | Match? |
|---|---|
| <div> | Match |
| <img src="test.jpg" /> | Match |
| <a href="#">link</a> | Match |
| just text | No match |
Common Use Cases
- HTML parsing and extraction
- Tag counting
- Content scraping
- Template analysis
Variations
<\/?[a-zA-Z][^>]*>Matches both opening and closing tags
Other Regex Patterns
Want to test this pattern with your own data?
Open the Regex Tester