$devtoolkit.sh/examples/regex/html-tags

Regex Pattern for HTML Tags

Matching HTML tags with regex is useful for stripping markup from text content or finding specific elements in a string. This example matches opening, closing, and self-closing tags along with their attributes. Be aware that regex is not a reliable HTML parser for complex, nested structures — use a proper DOM parser for production HTML processing. The tester here is ideal for quick text transformations.

Example
/<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)/gi

# Test input:
<p class="intro">Hello world</p>
<img src="logo.png" alt="Logo" />
<a href="https://example.com">Click here</a>
<br />
<div id="main"><span>Nested</span></div>
[ open in Regex Tester → ]

FAQ

Why should I not parse HTML with regex?
HTML allows arbitrary nesting, optional quotes, and malformed markup that breaks regex. For robust HTML processing, use the browser DOM API or a parser like cheerio.
How do I strip all HTML tags from a string?
Use a simple pattern like /<[^>]*>/g with replace to remove all tag-like substrings. This works for clean HTML but may fail on malformed or nested edge cases.
What does the i flag do in regex?
The i flag enables case-insensitive matching, so the pattern matches both <P> and <p>. HTML tags are case-insensitive in the HTML specification.

Related Examples

/examples/regex/html-tagsv1.0.0