How to Remove HTML Tags from Text (JavaScript, Python, Regex)

4 min read

Stripping HTML tags is one of the most common text processing tasks, whether you're cleaning CMS output, scraping websites, or preparing text for analysis.

JavaScript

// DOM method (safest)
function stripHtml(html) {
  const doc = new DOMParser().parseFromString(html, 'text/html');
  return doc.body.textContent || '';
}

// Regex method (simple)
const text = html.replace(/<[^>]*>/g, '');

Python

from html.parser import HTMLParser
from io import StringIO

class MLStripper(HTMLParser):
    def __init__(self):
        super().__init__()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

# Or simply:
import re
re.sub(r'<[^>]*>', '', html)

Online Tool

Use our plain text converter to strip HTML instantly.