Back to the tool

Regex to Remove HTML Tags: the Pattern, Its Limits, and the Parser Line

The regex to remove HTML tags from a string is <[^>]+>. That handles the overwhelming majority of tag-stripping jobs. What follows is why it works, where it breaks, and the one thing you should never use it for.

The pattern is <[^>]+>. That is the whole answer, and it handles the overwhelming majority of tag-stripping jobs: opening tags, closing tags, self-closing tags, tags with attributes. If you came here from a search for a regex to remove HTML tags from a string, you can stop reading and use it. What follows is why it works, where it breaks, and the one thing you should never use it for.

Read the pattern piece by piece. < matches a literal opening angle bracket. [^>] is a negated character class: any character except a closing angle bracket. + means one or more of those. > matches the literal closing bracket. So the pattern says: find a <, then keep eating characters until you hit the first >. That is a tag, almost by definition.

In JavaScript:

const clean = html.replace(/<[^>]+>/g, "");

In Python:

import re
clean = re.sub(r"<[^>]+>", "", html)

The g flag (or re.sub, which replaces all by default) matters. Without global replacement you strip the first tag and keep the rest, which is a classic copy-paste bug.

Regex to remove HTML tags: why not just use <.*?>

You will see <.*?> suggested, with the lazy .*?. It works on simple input, but the negated class is better for two reasons. First, it is explicit: "everything up to the next >" is exactly what a tag is, while .*? says "as little as possible" and relies on the regex engine's backtracking to figure out what that means. Second, on pathological input the lazy dot version can backtrack badly, while [^>]+ cannot backtrack past a > it already consumed. For a pattern this small the performance difference rarely matters, but the clarity difference always does: the next person reading your code sees what a tag is.

Now the limits, stated honestly, because this is where regex-for-HTML answers usually lie by omission. The pattern breaks if an attribute value contains a >, as in <div title="a > b">. It does not understand comments, CDATA sections, or script blocks, so stripping tags from a full page can leave JavaScript behind. It leaves HTML entities untouched: &amp; stays &amp; until you decode it separately. And it cannot tell you anything about structure. It deletes tags; it does not parse them.

That last point is the real boundary. Stripping tags from scraped text, cleaning user input for a plain-text preview, removing formatting before indexing: all fine, and this pattern is the right tool. Extracting the third link in a nav, finding all images inside a specific div, validating that markup is well-formed: not fine. That is parsing, and parsing needs a parser. In Python that means BeautifulSoup or lxml; in JavaScript, DOMParser. My rule: if you need to know what the tags mean, put the regex down.

One more practical note. If you are stripping tags for display, strip first and then trim whitespace, because removing block tags often leaves doubled spaces and blank lines. And if the source is untrusted user input, remember that tag-stripping is not sanitization: an event handler attribute is gone with its tag, but a javascript: URL in a tag you chose to keep is not. Sanitization is a separate job with separate tools. Generate and test variations of this pattern against your own edge cases with the regex template builder before you ship it.

Frequently asked questions

What does each part of <[^>]+> mean?

< matches a literal opening bracket. [^>] is a negated character class meaning any character except a closing bracket. + means one or more of those. > matches the literal closing bracket. Together: find a <, eat everything up to the first >, which is a tag almost by definition.

Why not just use <.*?>?

The lazy-dot version works on simple input, but the negated class is explicit about what a tag is and cannot backtrack past a > it already consumed. The clarity difference matters more than the performance difference: the next person reading your code sees the definition of a tag.

Does it handle nested tags?

For removal, yes: stripping all tags from

hi

leaves hi regardless of nesting, because every tag matches independently. But it cannot tell you anything about the structure, which tags contain which. That is parsing, and it needs a parser.

What about HTML entities like &?

The pattern leaves entities untouched, so & stays & until you decode it separately. Tag-stripping and entity-decoding are two steps. And remember: stripping tags is not sanitization, which is a separate job with separate tools.

Related reading: Regex Greedy vs Lazy Matching: The One Question Mark That Changes Everything · Catastrophic Backtracking: The Regex That Took Down Cloudflare for 27 Minutes · Why Most Email Regex Patterns Are Wrong (And What to Use Instead) · Regex Lookahead and Lookbehind Explained: The Zero-Width Trick · Regex Word Boundaries Explained: the Two Characters That Stop False Matches

Try the tool

Start from a working template instead of a blank page. Customize a regex template with plain-English explanations of what each part does.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.