<\/?[a-zA-Z][^>]*> This pattern matches HTML opening and closing tags: a <, an optional / for closing tags, a letter-starting tag name, any attributes up to the closing >. It is a lightweight tag finder, not a full HTML parser. Web patterns deal with markup, styling and web identifiers: HTML tags and comments, CSS colors, Markdown links, slugs and source-code comments. They are handy for quick scanning and lightweight transforms, but markup is not a regular language, so anything structural or security-sensitive should use a real parser (DOM, CSS or Markdown) rather than regex alone. It uses the g flag(s). Apply it across text to collect every occurrence. It cannot handle > inside quoted attribute values correctly and does not parse nested structure — use a real HTML parser for DOM work. Comments, doctypes and CDATA are not matched by this tag pattern. Regex-based HTML manipulation is fragile; prefer DOM APIs (DOMParser, BeautifulSoup) for anything beyond simple scanning. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.
Token-by-token explanation
< — literal opening angle bracket\/? — optional slash for closing tags[a-zA-Z] — tag name must start with a letter[^>]* — any characters except > (the attributes)> — literal closing angle bracketg flag — find all tags in the document
What it matches
<div></div><a href="x"><br/><img src='y'> What it does not match
<1invalid><>div><!-- comment --> When to use it
Reach for a web pattern for quick scanning and lightweight transforms of markup, styling and web identifiers: locating tags, comments, colors, slugs or Markdown links. For anything structural or security-sensitive — parsing a DOM, sanitizing HTML, rewriting CSS — use a real parser or a proven library instead, because markup is not a regular language and regex-based handling is a frequent source of bugs and cross-site scripting bypasses.
Caveats
- It cannot handle > inside quoted attribute values correctly and does not parse nested structure — use a real HTML parser for DOM work.
- Comments, doctypes and CDATA are not matched by this tag pattern.
- Regex-based HTML manipulation is fragile; prefer DOM APIs (DOMParser, BeautifulSoup) for anything beyond simple scanning.
Code examples
const re = /<\\/?[a-zA-Z][^>]*>/g;
const text = "see <div> and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence import re
rx = re.compile(r"<\/?[a-zA-Z][^>]*>")
text = "see <div> and more"
found = rx.findall(text) # every occurrence (groups if present) import java.util.regex.*;
Pattern p = Pattern.compile("<\\/?[a-zA-Z][^>]*>");
Matcher m = p.matcher("<div>");
boolean ok = m.find(); // scan for occurrences <?php
$subject = "<div>";
if (preg_match_all('/<\\/?[a-zA-Z][^>]*>/', $subject, $m)) {
// $m[0] is the full match; $m holds every occurrence
} How to test it
Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.
Frequently asked questions
Why not parse HTML with regex?
HTML is not a regular language: nesting, quoted attributes containing >, comments and malformed markup break simple patterns. Use a proper parser for correctness and security.
Does it match self-closing tags like <br/>?
Yes — [^>]* consumes the trailing slash before >, so <br/> and <img .../> are matched along with normal open/close tags.
When should I avoid regex for web content?
For parsing HTML, CSS or Markdown structure, use a real parser. Regex cannot handle nesting, quoted delimiters or malformed markup reliably, and regex-based sanitization is a common source of XSS bypasses.
Why is regex HTML sanitization dangerous?
HTML has nested, quoted and malformed constructs that regex mis-parses, so attackers hide scripts in forms the pattern misses. Always sanitize with a proven library (DOMPurify) or a real parser, never ad-hoc regex.
Try the Regex Tester
Live regex matching with groups, flags and explanations.