Extraction
Match an HTML tag
Flags: g
Extract simple HTML tags for lightweight text processing.
Pattern
Regular expression
<\/?[A-Za-z][A-Za-z0-9]*\b[^>]*>How it works
Matches opening or closing HTML tags of any name (`<a>`, `<div class="x">`, `</span>`, `<br/>`). Does not attempt to parse attributes or nested content.
Matches
- ▸<div>
- ▸<a href="/x" class="b">
- ▸</span>
- ▸<br/>
Non-matches
- ▸< 1 (space after `<`)
- ▸just text
Common gotchas
Do not use this to parse arbitrary HTML — the language is not regular. Use a DOM parser (`DOMParser` in browsers, BeautifulSoup in Python, Goquery in Go). This pattern is fine for quick text stripping and log processing.
Language snippets
JavaScript
const re = /<\/?[A-Za-z][A-Za-z0-9]*\b[^>]*>/g;
text.replace(re, ""); // strip tagsPython
re.sub(r"<\/?[A-Za-z][A-Za-z0-9]*\b[^>]*>", "", text)Go
re := regexp.MustCompile(`<\/?[A-Za-z][A-Za-z0-9]*\b[^>]*>`)
re.ReplaceAllString(text, "")Java
text.replaceAll("<\\/?[A-Za-z][A-Za-z0-9]*\\b[^>]*>", "")Frequently asked questions
What is the regex for HTML tag?
The pattern <\/?[A-Za-z][A-Za-z0-9]*\b[^>]*> (flags: g) matches HTML tag. Matches opening or closing HTML tags of any name (`<a>`, `<div class="x">`, `</span>`, `<br/>`). Does not attempt to parse attributes or nested content.
What does this HTML tag pattern match?
It matches strings like <div>, <a href="/x" class="b">, </span>, but rejects < 1 (space after `<`), just text.
How do I use this pattern in JavaScript, Python, Go, Java?
Ready-to-run snippets are provided for JavaScript, Python, Go, Java. Copy the one for your language from the Language snippets section — each wraps the same core pattern in that language's regex API.
What are common pitfalls with a HTML tag regex?
Do not use this to parse arbitrary HTML — the language is not regular. Use a DOM parser (`DOMParser` in browsers, BeautifulSoup in Python, Goquery in Go). This pattern is fine for quick text stripping and log processing.