Strip HTML Tags From Text: Manual and Regex Methods
To strip HTML tags, the easiest route is not to touch the markup at all: open the page in a browser, select the visible text and paste it as plain text. When you only have the source, a regular expression such as <[^>]+> removes most tags, and a parser handles the rest. This guide shows how to strip HTML tags by hand, what the regex misses, and when to switch to a proper HTML parser.
What counts as an HTML tag
An HTML tag is a piece of markup inside angle brackets, such as <p>, </p>, <a href="..."> or <br>. Tags tell a browser how to display the words between them. When you copy source code instead of the rendered page, you get the tags along with the words, and removing them means keeping the text between the brackets and deleting the brackets and everything inside.
Three other things look similar and need separate handling:
- HTML entities such as
&, and<. They are not tags. They stay in the text after the tags are gone. - Comments such as
<!-- note -->. - Script and style blocks. The tags around them are removed, but the code between the tags is left behind as text unless you remove the whole block.
Method 1: copy the visible text
If the page is online or you can open the file in a browser, skip the markup entirely.
- Open the page or HTML file in a browser.
- Select the text you want.
Ctrl+A(Cmd+Aon a Mac) selects the whole page. - Copy, then paste with
Ctrl+Shift+V(Cmd+Shift+Von a Mac) into a plain-text destination, or paste into Notepad or TextEdit in plain-text mode.
The browser has already done the parsing, so entities are decoded, scripts are hidden and comments are gone. This is the most reliable method for a single page.
For a quick one-off in the browser console, document.body.innerText returns the rendered text of the page, and you can copy it from there.
Method 2: strip HTML tags with a regex in your editor
When all you have is a snippet, a find-and-replace with regular expressions turned on is quick. In Visual Studio Code, Notepad++ and Sublime Text, switch on the regular expression option in the find box, then:
| Goal | Find | Replace with |
|---|---|---|
| Remove all tags | <[^>]+> | nothing |
| Replace paragraph and line-break tags with a newline first | </p>|<br\s*/?> | \n |
| Remove comments (do this before the tag step) | <!--[\s\S]*?--> | nothing |
| Remove script and style blocks (do this before the tag step) | <(script|style)\b[\s\S]*?</\1> | nothing |
The main pattern reads as: an opening angle bracket, then one or more characters that are not a closing angle bracket, then a closing angle bracket. Run the specific patterns first, because the general pattern would break comments and script blocks into pieces. Whether the replacement understands \n as a line break depends on the editor, so check on a small sample.
Without the paragraph step, the words from separate paragraphs run together, because removing </p> deletes the only thing separating them.
Decode the entities second
Strip the tags before you decode the entities, never after. If you decode first, the entity <b> becomes the characters <b>, and the tag pattern then deletes text that the author meant to show as a literal tag. After the tags are gone, replace the common entities:
| Entity | Becomes |
|---|---|
& | & |
< | < |
> | > |
" | " |
| a non-breaking space (U+00A0) |
The last row deserves a second look. Decoding produces a real non-breaking space, which looks like an ordinary space and is not one. It is a classic source of text that fails an exact match after the tags have been stripped. Our guides to HTML space and the non-breaking space explain it, and the cleaner turns it back into a normal space.
When a regex is not enough
A regex works on the shape of the text and has no idea what the HTML means. It goes wrong in predictable places:
- A stray bracket in the text. A sentence containing
3 < 5as an unescaped character can make the pattern swallow everything up to the next>. - A bracket inside an attribute value. A tag like
<a title="a > b">ends too early at the first>. - Unusual constructs such as CDATA sections, conditional comments and embedded SVG.
- Layout lost. Tables and lists collapse into one run of words.
It is also not a safety measure. If your goal is to make untrusted HTML safe to display, stripping tags with a regex is not enough, and a sanitising library built for that job is the right tool.
Method 3: use a parser
For anything beyond a snippet, let a real HTML parser build the document and read its text. It deals with entities, comments and malformed markup correctly.
In the browser
const doc = new DOMParser().parseFromString(html, "text/html");
const text = doc.body.textContent;
DOMParser does not run scripts in the markup it parses. Note that textContent includes the contents of script and style elements, so remove those elements first if they are present.
In Python
The Beautiful Soup library does this in two lines:
from bs4 import BeautifulSoup
text = BeautifulSoup(html, "html.parser").get_text(separator=" ")
The standard library has html.parser for custom handling, and html.unescape() to decode entities in a string you have already stripped.
On the command line
Text-mode browsers such as lynx and w3m can dump a page as plain text, for example lynx -dump -nolist page.html.
After the tags are gone
Stripped HTML usually comes out with long runs of spaces and blank lines where indentation and line breaks used to sit. Paste the result into the AI Text Cleaner. It collapses repeated spaces, reduces runs of blank lines to one, trims the ends and clears invisible characters. It will not remove tags itself, so do that step first.
Quick answers
What is the regex to remove HTML tags? <[^>]+> removes most tags from simple markup. It fails on brackets inside attributes and leaves script contents behind.
Can I strip HTML tags in Word? Paste the rendered page into Word with Keep Text Only, or use the browser method above. Do not paste source code and expect Word to hide the tags.
Why do I still see after stripping? Entities are not tags. Decode them in a second step, after the tags are removed.