Regex to Match HTML Tags

Match HTML tags including opening, closing, and self-closing tags with this regex. Captures the tag name and optional attributes. Free regex tool.

Regular Expression

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

Token Breakdown

TokenDescription
<Matches the literal character '<'
\/Matches a literal forward slash
?Makes the preceding element optional (zero or one times)
(Start of capturing group
[a-zA-Z]Character class — matches any one of: a-zA-Z
[a-zA-Z0-9]Character class — matches any one of: a-zA-Z0-9
*Matches the preceding element zero or more times (greedy)
)End of group
(?:Start of non-capturing group
\sMatches any whitespace character (space, tab, newline)
[^>]Negated character class — matches any character NOT in >
*Matches the preceding element zero or more times (greedy)
)End of group
?Makes the preceding element optional (zero or one times)
>Matches the literal character '>'

Detailed Explanation

This regex matches HTML tags including opening, closing, and self-closing tags. Here is the token-by-token breakdown:

< — Matches the literal opening angle bracket that begins every HTML tag.

/? — Matches an optional forward slash for closing tags like

. The backslash escapes the slash, and the ? makes it optional so both opening and closing tags match.

([a-zA-Z][a-zA-Z0-9]*) — A capturing group that matches the tag name. The first character must be a letter (HTML tags always start with a letter), followed by zero or more letters or digits. This captures tag names like 'div', 'h1', 'span', 'input', etc.

(?:\s[^>])? — An optional non-capturing group for attributes. It starts with a whitespace character (\s) followed by zero or more characters that are not a closing angle bracket ([^>]). This loosely matches any attributes within the tag.

— Matches the literal closing angle bracket.

The g flag enables global matching to find all tags in the HTML text. This pattern is intentionally simple and works well for most common HTML. It captures the tag name in the first group, making it easy to extract. However, regex is generally not recommended for parsing complex or malformed HTML. For robust HTML processing, a proper DOM parser is preferred.

This pattern is useful for quick analysis tasks like counting tags, finding specific elements, or syntax highlighting in developer tools.

Example Test Strings

InputExpected
<div>Match
</span>Match
<input type="text" />Match
not a tagNo Match
<h1 class="title">Match

Try It — Interactive Tester

//g
gimsuy

Match Highlighting(4 matches)

<div> </span> <input type="text" /> not a tag <h1 class="title">

Matches & Capture Groups

#1<div>index 0
Group 1:div
#2</span>index 6
Group 1:span
#3<input type="text" />index 14
Group 1:input
#4<h1 class="title">index 46
Group 1:h1
Pattern: 39 charsFlags: gMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →