Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for HTML tag

Match HTML opening, closing, or self-closing tags.

The HTML tag regex is <\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\/?> — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

<\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\/?>
Try in explainer → Download cheat sheet ↓

What it matches

  • <div>
  • </div>
  • <img src="cat.jpg" />
  • <a href="https://example.com">

What it doesn't match

  • < div>
  • <div
  • plain text

Notes & gotchas

Matches a single HTML tag with optional attributes. Does NOT parse nested structure — for that, use a real HTML parser (DOMParser, BeautifulSoup, etc.).

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
<literal text “<”
\/?optional (zero or one): literal “/”
[a-zA-Z]any of: lowercase letters, uppercase letters
[a-zA-Z0-9]*zero or more: any of: lowercase letters, uppercase letters, digits
(start of a capturing group
\s+one or more: whitespace
[a-zA-Z-]+one or more: any of: lowercase letters, uppercase letters, “-”
(start of a capturing group
\s*zero or more: whitespace
=literal text “=”
\s*zero or more: whitespace
(start of a capturing group
"literal text “"”
[^"]*zero or more: any character except “"”
"literal text “"”
|OR — try the alternative
\'literal “'”
[^\']*zero or more: any character except “'”
\'literal “'”
|OR — try the alternative
[^\s>]+one or more: any character except whitespace, “>”
)end of group
)?end of group, optional (zero or one)
)*end of group, zero or more
\s*zero or more: whitespace
\/?optional (zero or one): literal “/”
>literal text “>”

About this pattern

Developer-oriented patterns help with code analysis, log parsing, and data transformation. Use these in build scripts, CI tools, and code review utilities.

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /<\\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\\/?>/.test(value)
  • Python: re.match(r"""<\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\/?>""", value)
  • Java: Pattern.compile("<\\/?[a-zA-Z][a-zA-Z0-9]*(\\s+[a-zA-Z-]+(\\s*=\\s*(\"[^\"]*\"|\\'[^\\']*\\'|[^\\s>]+))?)*\\s*\\/?>").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"<\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*(""[^""]*""|\'[^\']*\'|[^\s>]+))?)*\s*\/?>")
  • Go: regexp.MustCompile(`<\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\/?>`).MatchString(value)
  • Ruby: /<\\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\\/?>/.match?(value)
  • PHP: preg_match('~<\/?[a-zA-Z][a-zA-Z0-9]*(\s+[a-zA-Z-]+(\s*=\s*("[^"]*"|\'[^\']*\'|[^\s>]+))?)*\s*\/?>~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Not anchored. Without ^ and $ this can match a substring anywhere in the input — add anchors if you need the whole value to conform.
  • ASCII letters only. [a-zA-Z] excludes accented and non-Latin letters (é, ü, ß, ñ, and non-Latin scripts). For international input use Unicode properties like \p{L} with the u flag.
  • Watch for backtracking. This pattern nests quantifiers; on adversarial input that can cause exponential backtracking (ReDoS). Test with long non-matching strings, or use an RE2-based engine.
  • Escape it correctly per language. In Java and JavaScript strings each backslash must be doubled (\\d); in Python, Go, and C# use raw/verbatim strings so the backslashes survive.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the html tag against a source of truth (database, API, or checksum) where it matters.

Standards & sources

This pattern is based on the following authoritative specification(s) and issuing authorities. Formats can change — always confirm against the primary source.

Related patterns

More patterns in the Developer category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer for a token-by-token breakdown, live testing, and code in seven languages.


Want more patterns? Browse the full library →