stripHTMLTags
Removes all HTML tags from a string.
1/**
2 * Removes all HTML tags from a string.
3 * Useful for sanitizing content or extracting plain text.
4 *
5 * @param str - The input string containing HTML.
6 * @returns A string with all HTML tags removed.
7 */
8export function stripHTMLTags(str: string): string {
9 return str.replace(/<[^>]*>/g, '');
10}
Quick Plain Text Extraction
Efficiently removes all HTML tags, making it ideal for extracting clean, readable content from HTML sources.
No Dependencies
Lightweight and self-contained — doesn't require a DOM parser or third-party library.
Versatile and General-Purpose
Works for a variety of HTML content types (emails, CMS content, snippets), and can be used in both frontend and backend environments.
Simple Implementation
Uses a single regular expression that's easy to understand and modify for most common HTML cleanup needs.
Tests | Examples
1test('stripHTMLTags - removes simple HTML tags', () => {
2 expect(stripHTMLTags('<p>Hello <strong>world</strong></p>')).toBe('Hello world');
3});
4
5test('stripHTMLTags - handles nested tags', () => {
6 expect(stripHTMLTags('<div><span>Text</span></div>')).toBe('Text');
7});
8
9test('stripHTMLTags - leaves plain text untouched', () => {
10 expect(stripHTMLTags('Just text')).toBe('Just text');
11});
12
13test('stripHTMLTags - works with self-closing tags', () => {
14 expect(stripHTMLTags('<br/>Line<br/>')).toBe('Line');
15});
16
17test('stripHTMLTags - works with attributes and mixed content', () => {
18 expect(stripHTMLTags('<a href="#">Link</a> © <span class="note">2025</span>'))
19 .toBe('Link © 2025');
20});
21
22test('stripHTMLTags - handles empty string', () => {
23 expect(stripHTMLTags('')).toBe('');
24});
Common Use Cases
Sanitizing User-Generated Content
Convert HTML-formatted input to plain text for display in secure or simplified contexts (e.g., previews, logs).
Generating Metadata
Strip tags when producing excerpts, summaries, or SEO descriptions from HTML content.
Exporting Clean Text
Create downloadable versions of documents (like PDFs or plain-text emails) that don’t include markup.
Search Indexing or Text Analysis
Normalize HTML content before feeding it into a search engine or natural language processing pipeline.