Skip to content

How to Strip HTML Tags and Extract Plain Text

What Are HTML Tags?

HTML tags are the markup codes that define the structure and formatting of web content. Tags are enclosed in angle brackets: <h1> defines a heading, <p> defines a paragraph, <a href="..."> defines a link, <strong> defines bold text. When you view a web page, you see the rendered content — the text, images, and layout. The underlying HTML tags are hidden by the browser but present in the source code.

Strip all HTML tags from any content with WritePadPro's Strip HTML Tags tool — paste HTML and get clean plain text with all markup removed instantly.

Stripping HTML tags means removing all the markup codes while preserving the visible text content. The HTML <h1>Hello <strong>World</strong></h1> becomes the plain text "Hello World" — the tags are removed, the text remains.

Why Strip HTML Tags?

There are many practical scenarios where you need plain text extracted from HTML content.

Email Cleanup

HTML emails contain extensive markup — tables for layout, inline styles for formatting, tracking pixels, and embedded images. When you need the text content of an email for analysis, quoting, or plain-text forwarding, stripping the HTML extracts just the readable content. A 50 KB HTML email might contain only 500 words of actual text buried in 45 KB of markup.

CMS Migration

Moving content between content management systems (WordPress to Shopify, Drupal to HubSpot, custom CMS to any platform) often requires extracting plain text from the source system's HTML. Different CMS platforms use different HTML structures, classes, and formatting conventions. Stripping tags produces clean text that can be reformatted in the destination system without carrying over incompatible markup.

Data Extraction and Scraping

Web scraping retrieves HTML pages, but the useful content is the text within the tags, not the tags themselves. Product descriptions, article content, review text, and pricing information must be extracted from their HTML wrappers for analysis, storage, or re-publishing. For encoding the extracted content safely, see the HTML Encoder & Decoder.

SEO Content Analysis

Analyzing the actual text content of a page — word count, readability, keyword density — requires stripping HTML first. Tags like <div> and <span>, attributes like class="...", and inline styles add characters that are not visible content. Accurate text analysis operates on the stripped plain text, not the raw HTML source.

Word Counting Raw Content

If you paste HTML source code into a word counter, every tag is counted as words — <div class="container"> registers as 2 words that are not actual content. Stripping tags first ensures your word count reflects only the visible text. For clean word counting after stripping, see our Remove Extra Spaces Guide — stripping tags often leaves behind extra whitespace.

Accessibility and Plain Text Versions

Some systems require plain text alternatives: email campaigns need both HTML and plain text versions, accessibility compliance may require plain text alternatives for complex HTML, and some API endpoints accept only plain text. Stripping tags produces the plain text version from existing HTML content.

What Gets Removed vs What Stays

Removed (Tags and Attributes)

  • All opening tags: <h1>, <p>, <div>, <span>, <a href="...">
  • All closing tags: </h1>, </p>, </div>
  • Self-closing tags: <br />, <img src="..." />, <hr />
  • All attributes within tags: class, id, style, href, src, data-*
  • Comments: <!-- comment -->
  • Script and style blocks: <script>...</script>, <style>...</style>

Preserved (Text Content)

  • All visible text between tags
  • Text within links (the anchor text, not the URL)
  • Text within formatting tags (bold text, italic text — the words, not the formatting)
  • HTML entities (decoded to their character equivalents): &amp; becomes &, &lt; becomes the less-than sign

Edge Cases

  • Alt text: Image alt attributes (<img alt="description">) are lost when tags are stripped. If you need alt text preserved, read the alt attributes with a DOM-based script; WritePadPro’s tool, like most strippers, leaves them out.
  • Link URLs: The text "Click here" is preserved, but the URL in href="https://..." is lost. For content where URLs matter, extract them separately — or, in WritePadPro’s tool, tick Keep link URLs to write each link as “link text (URL)”.
  • Table data: Table markup is removed, but cell content is preserved. A simple stripper turns a table into a flat text sequence that may be hard to interpret; WritePadPro’s tool, with Keep line breaks on, puts each row on its own line and separates cells with tabs.

For viewing HTML with tags preserved rather than stripped, use the HTML Preview tool. For related cleanup after stripping, see our Remove Punctuation Guide.

Regex vs DOM Parsing: Two Approaches

There are two fundamentally different technical approaches to stripping HTML tags, each with distinct trade-offs.

Regex Approach (Simple but Fragile)

The most common quick method uses a regular expression to match and remove anything between angle brackets:

text.replace(/<[^>]+>/g, "")

This pattern matches any sequence starting with < and ending with >, and replaces it with nothing. It is fast, simple, and works for most well-formed HTML.

Limitations:

  • Does not handle malformed HTML (tags missing closing brackets)
  • Does not remove content inside script and style tags — it removes the tags but leaves the JavaScript/CSS code as visible text
  • Does not decode HTML entities (leaves &amp; as literal text instead of converting to &)
  • Can be tricked by angle brackets in text content or attributes

DOM Parsing Approach (Robust but Heavier)

The browser's built-in DOM parser understands HTML structure. Use it to extract text content:

// JavaScript
var doc = new DOMParser().parseFromString(htmlString, "text/html");
doc.querySelectorAll("script, style").forEach(function (el) { el.remove(); });
var text = doc.body.textContent;

This approach:

  • Handles malformed markup the same way a browser does
  • Lets you drop script and style elements together with their content (textContent on its own would keep that code as text)
  • Automatically decodes HTML entities
  • Handles nested tags, self-closing tags, and comments correctly
  • Parses into an inert document, so scripts in the pasted HTML do not run and images are not loaded. Assigning untrusted HTML to innerHTML of an element on the live page, as many snippets do, can fire handlers such as onerror.

WritePadPro uses the DOM parsing approach for accurate, reliable tag stripping.

Language-Specific Methods

LanguageMethod
JavaScriptnew DOMParser().parseFromString(html, "text/html").body.textContent
Pythonfrom bs4 import BeautifulSoup; BeautifulSoup(html, "html.parser").get_text()
PHPstrip_tags($html) (simple tag removal) or DOMDocument (DOM-based)
JavaJsoup.parse(html).text()
C#HtmlAgilityPack: doc.DocumentNode.InnerText
RubyNokogiri::HTML(html).text

Security: Stripping Tags for XSS Prevention

Stripping HTML tags is sometimes used as a security measure to prevent Cross-Site Scripting (XSS) attacks — but it is NOT a reliable security method by itself.

Why Tag Stripping Alone Is Insufficient

  • Attribute-based attacks: <img src=x onerror=alert(1)> — the tag is an image tag, but the onerror attribute executes JavaScript. Simple tag stripping that allows <img> tags leaves this attack vector open.
  • URL-based attacks: <a href="javascript:alert(1)">Click</a> — the tag is a link, but the href contains executable JavaScript.
  • CSS-based attacks: <div style="background:url(javascript:alert(1))"> — older browsers executed JavaScript in CSS URLs.
  • Incomplete stripping: Nested or malformed tags can bypass regex-based strippers.

The Correct Security Approach

For XSS prevention, use HTML entity encoding (converting dangerous characters to their entity equivalents) rather than tag stripping. Encoding < to &lt; and > to &gt; makes all HTML harmless without removing any content. Most modern web frameworks and template engines encode output automatically. For encoding details, see our HTML Entities Guide.

Tag stripping is appropriate for content extraction (getting plain text from HTML). It is NOT appropriate as a sole security measure against XSS. For detecting hidden characters that might bypass security filters, see our Invisible Characters Guide.

Post-Stripping Cleanup

After stripping HTML tags, the resulting text typically needs additional cleanup.

Extra Whitespace

With simple strippers, HTML tags that occupied their own lines leave behind blank lines, and source indentation turns into stray spaces. WritePadPro’s tool collapses source whitespace the way a browser does and keeps at most one blank line between blocks, so its output is usually tidy already. If you still need to, run the text through Remove Extra Spaces or Remove Empty Lines.

Residual Entities

If using regex-based stripping (not DOM parsing), HTML entities like &nbsp;, &amp;, &mdash; remain as literal text instead of being decoded. DOM-based stripping (which WritePadPro uses) decodes these automatically.

Orphaned Formatting

Content that relied on HTML structure for meaning — tables, lists, multi-column layouts — loses its structural context when tags are stripped. A simple stripper turns a table into a flat sequence of cell values (WritePadPro keeps one row per line, with tab-separated cells), and a two-column layout can become interleaved content. Review stripped content for structural elements that need manual reorganization.

Using WritePadPro's Strip HTML Tags Tool

WritePadPro's Strip HTML Tags tool extracts clean plain text from any HTML content.

Step 1: Paste Your HTML

Open the Strip HTML Tags tool and paste your HTML source code — from web pages, email source, CMS exports, or any HTML content.

Step 2: Choose Options and Strip Tags

Two options shape the output. Keep line breaks (on by default) turns paragraphs, headings, list items, table rows and <br> tags into new lines; untick it to join everything into one line. Keep link URLs writes each link as “link text (URL)”. Click Strip Tags. The tool parses the HTML with the browser’s DOM parser, drops script, style, noscript, template, iframe, object and embed elements with their content, decodes entities, and extracts only the visible body text. The plain text appears in a separate output box, and your original HTML stays untouched.

Step 3: Review the Results

The tool shows statistics:

  • HTML tags removed — how many tags were stripped
  • Script/style blocks removed — non-visible elements dropped with their content
  • Characters before → after — the size of the HTML compared with the plain text
  • Words in output and Lines in output

Step 4: Clean Up and Use

Copy the plain text or download it as a .txt file. The extracted text is ready for word counting, readability analysis, content migration, or any plain text use case.

Privacy

All processing runs locally in your browser using the DOM parser. Your HTML content — including email source code, proprietary templates, and scraped data — is never transmitted to any server.

Summary

Stripping HTML tags extracts the visible text content from HTML markup, removing all tags, attributes, comments, and script/style blocks. The two main approaches are regex-based (simple but fragile) and DOM parsing (robust and accurate). WritePadPro uses DOM parsing for reliable results.

Common use cases include email cleanup, CMS migration, data extraction, SEO content analysis, and creating plain text versions of HTML content. Remember: tag stripping is for content extraction, not security. For XSS prevention, use HTML entity encoding instead.

Extract plain text from HTML with WritePadPro's Strip HTML Tags tool — DOM-based parsing in your browser with complete privacy. Follow up with the Remove Extra Spaces Guide for post-stripping whitespace cleanup.

Frequently Asked Questions

Does stripping HTML tags remove the text inside the tags?

No — stripping removes only the tags themselves (the markup codes in angle brackets), not the text content between them. The HTML '<strong>Hello World</strong>' becomes 'Hello World' — the bold formatting tag is removed but the words are preserved. The exception is script and style blocks: WritePadPro removes both the tags AND the content inside script and style elements (and noscript, template, iframe, object and embed elements), since JavaScript code and CSS rules are not meaningful as plain text content.

Is stripping HTML tags safe for preventing XSS attacks?

No — tag stripping alone is NOT a reliable security measure against Cross-Site Scripting (XSS). Attackers can bypass simple tag stripping using attribute-based attacks (onerror, onload), javascript: URLs in href attributes, CSS expression attacks, and malformed HTML that confuses the stripper. The correct approach for XSS prevention is HTML entity encoding — converting characters such as < and > into HTML entities so that all HTML becomes harmless visible text. Most modern web frameworks encode output automatically. Use tag stripping for content extraction, entity encoding for security.

What is the difference between regex and DOM parsing for tag removal?

Regex-based stripping uses a pattern like /<[^>]+>/g to match and remove anything between angle brackets. It is fast and simple but fragile — it fails on malformed HTML, leaves script/style content as visible text, does not decode HTML entities, and can be tricked by angle brackets in attribute values. DOM parsing uses the browser's built-in HTML parser to construct a document tree, then extracts the text content. It handles malformed and nested markup the way a browser does, decodes entities, and lets you drop script and style elements with their content. WritePadPro uses DOM parsing for accuracy.

Why does stripped text have extra whitespace?

With simple strippers, HTML tags that occupied their own lines leave behind empty lines when removed. Block-level elements like div, p, h1-h6, and li that were on separate source code lines create blank line gaps in the stripped text. Additionally, HTML collapses all whitespace to single spaces when rendering, but the source code may contain tabs, multiple spaces, and newlines for readability — all of which become visible artifacts after stripping. WritePadPro's Strip HTML Tags tool avoids most of this: it collapses source whitespace the way a browser does and keeps at most one blank line between blocks, while <pre> blocks keep their spacing. For text from other strippers, run it through WritePadPro's Remove Extra Spaces tool, then Remove Empty Lines.

Try These Tools

Related Articles