Skip to content

stripHtml

Plain text of an HTML fragment: tags removed, <script>, <style> and comments dropped with their content, entities decoded (a common named subset, plus decimal and hex numeric ones) and whitespace collapsed and trimmed. Block-level tags and <br> count as a space, so <p>a</p><p>b</p> gives a b.

Not a sanitizer: this is for text extraction. Never use its output to make HTML safe, use escapeHtml for that.

Available since v3.1.4

import { stripHtml } from '@helpers4/string';
// or, from the all-in-one package (same code, one install):
import { stripHtml } from 'helpers4/string';
stripHtml(html: string): string
ParameterTypeDescription
htmlstringThe HTML fragment

string — The plain text

Removes tags and decodes entities, including   and numeric ones.

stripHtml('<p>Hello&nbsp;<b>world</b> &amp; co</p>')
// => 'Hello world & co'

Paragraphs, list items and line breaks become a space so words do not stick together.

stripHtml('<ul><li>one</li><li>two</li></ul>')
// => 'one two'

View source on GitHub