The simplest regex to remove HTML tags is <[^>]+>: find it and replace it with nothing, and every tag disappears while the text between tags stays. To remove one kind of tag but keep its content, such as span tags, find </?span\b[^>]*>. These patterns work well on simple, consistent markup that you control. They are not a full HTML parser, so script blocks, comments and a > inside an attribute value need extra handling, all covered below.
Here are the patterns for the common jobs, the cases where regex goes wrong on HTML, and how to run a tag clean-up across many files safely.
Remove all tags, keep the text
Find <[^>]+> and replace with nothing. Read it as: a <, then one or more characters that are not >, then a >. On <p>Hello <b>world</b></p> it leaves Hello world.
The negated class [^>]+ is what keeps each match to a single tag. The tempting alternative <.*> is greedy and would match from the first < on a line to the last >, deleting all the text in between. See greedy vs lazy regex for why.
Before you strip everything, remove the parts whose content you do not want either. Otherwise the JavaScript inside <script> and the CSS inside <style> are left behind as text. Run these first, in this order:
- Comments:
<!--[\s\S]*?--> - Script blocks:
<script\b[^>]*>[\s\S]*?</script> - Style blocks:
<style\b[^>]*>[\s\S]*?</style> - Then all remaining tags:
<[^>]+>
Use case-insensitive matching so <SCRIPT> is caught too. The [\s\S]*? parts match across lines, lazily, so each block ends at its own closing tag. More on this in matching everything between two strings.
Remove one tag, keep its content
Old pages often carry wrapper tags you no longer want: <font>, <span> from a word processor, or <center>. To remove both the opening and closing tag of one type:
| Remove | Find | Notes |
|---|---|---|
| All span tags | </?span\b[^>]*> | \b stops it matching <spanner> |
| All font tags | </?font\b[^>]*> | Typical in pages from old editors |
| Only spans with a certain class | <span class="highlight">([\s\S]*?)</span> → $1 | Fails if spans are nested inside it |
| Empty paragraphs | <p>\s*</p> | Also catches paragraphs with only spaces |
| Line break tags, to new lines | <br\s*/?> → a line break | Catches <br>, <br/> and <br /> |
The /? after < makes the slash optional, so one pattern removes both <span ...> and </span>. Because it removes opening and closing tags independently, it works even when spans are nested.
Remove an attribute, keep the tag
Inline styles and tracking attributes are common clean-up targets:
- Remove all inline styles: find
\s+style="[^"]*", replace with nothing.<p style="color:red" class="a">becomes<p class="a">. - Remove an
onclickhandler:\s+onclick="[^"]*" - Remove a
data-attribute:\s+data-track="[^"]*"
Including the leading \s+ removes the space before the attribute too, so no double spaces are left behind. If some attributes use single quotes, run a second rule with '[^']*'.
Where regex breaks on HTML
You will often read that you should never parse HTML with regex. The real rule is narrower: regex cannot understand HTML structure, so it works on predictable markup and fails on unusual markup. These are the cases that cause trouble:
| Input | <[^>]+> gives | Problem |
|---|---|---|
<a title="a > b">x</a> | b">x | A > inside an attribute ends the match early |
<p>if a < b then</p> | if a | A raw < in text starts a false "tag" |
<div class="ad"><div>x</div>y</div> with a lazy block pattern | y</div> | Nested tags of the same type end at the inner closing tag |
| A comment containing tags | Partial comment left behind | Remove comments first |
For the first case, a pattern that understands quoted values helps: <(?:[^>"']|"[^"]*"|'[^']*')*> skips over anything in quotes, so <a title="a > b"> is matched as one tag. The second case is invalid HTML (the < should be written <), and the third has no regex fix: for nested blocks of the same tag, edit those files by hand or use a tool that parses HTML.
Leftovers after stripping tags
Removing tags leaves the text, but not always in the shape you want:
- Entities.
&,<and stay as written. Replace the common ones afterwards if you need plain text, starting with&last so you do not double-decode. - Words run together.
<td>A</td><td>B</td>becomes AB. Replace block-level closing tags such as</p>,</td>and</li>with a space or line break before stripping the rest. - Blank lines and spaces. Indentation and empty lines from the markup remain. Clean them up with a blank-line pattern and a
{2,}to single space rule.
Sometimes you want to swap tags, not remove them
A clean-up often calls for replacing old tags with modern ones rather than deleting them. The same building blocks work, with a capture group for the optional slash:
<b>to<strong>: find<(/?)b>, replace<$1strong>. Both<b>and</b>are converted, and<br>is left alone because the pattern requires>straight after the b.<i>to<em>, keeping any attributes: find<(/?)i\b([^>]*)>, replace<$1em$2>.
Turn on case-insensitive matching if older pages use uppercase tags such as <B>.
Running a tag clean-up across many files
- Copy the folder, or commit it if it is under version control.
- Filter to the right file types, usually
.htmland.htm, and exclude templates you do not want touched. - Set up the rules in order: comments, scripts, styles, then specific tags or attributes. Order matters because each rule sees the result of the one before.
- Run a search first and look at a sample of matches, especially long ones.
- Replace, then open a few pages in a browser.
Bulk Replace runs stacked rules in order during one pass, which suits this kind of multi-step clean-up. Its before/after diff shows every removed tag in context, so a match that grabbed half a paragraph because of a stray > stands out, and you can untick it before anything is written. Bulk Replace backs up each file first, so the whole run can be undone in one click.
Quick reference
- All tags:
<[^>]+> - All tags, quote-aware:
<(?:[^>"']|"[^"]*"|'[^']*')*> - One tag type:
</?span\b[^>]*> - Comments:
<!--[\s\S]*?--> - Script blocks:
<script\b[^>]*>[\s\S]*?</script> - Inline styles:
\s+style="[^"]*"
Frequently asked questions
What is the simplest regex to remove all HTML tags?
<[^>]+> matches any tag. Replace it with nothing to leave the text, but scripts, styles and comments need separate handling.
How do I remove one tag but keep its content?
Find </?span\b[^>]*> and replace with nothing. That removes opening and closing span tags while keeping the text between them.
Why do people say not to parse HTML with regex?
HTML can nest, contain > inside attribute values and include comments, which regex cannot track reliably. It is fine for simple, consistent markup you control.
