striptags
PHP's strip_tags in Javascript
Last updated 6 years ago by ericnorris .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install striptags 
SYNC missed versions from official npm registry.

striptags (WIP)

An implementation of PHP's strip_tags in Typescript.

Highlights

  • No dependencies
  • Prevents XSS by default

Installing

npm install striptags

Basic Usage

// commonjs format
striptags(text: string, options?: Partial<StateMachineOptions>): string;

Examples

// commonjs format
const striptags = require("striptags").striptags;

// alternatively:
// import { striptags } from "striptags";

var html = `
<a href="https://example.com">lorem ipsum <strong>dolor</strong> <em>sit</em> amet</a>
`.trim();

console.log(striptags(html));
console.log(striptags(html, {allowedTags: new Set(["strong"])}));
console.log(striptags(html, {tagReplacementText: "🍩"}));

Outputs:

lorem ipsum dolor sit amet
lorem ipsum <strong>dolor</strong> sit amet
🍩lorem ipsum 🍩dolor🍩 🍩sit🍩 amet🍩

Advanced Usage

class StateMachine {
    constructor(partialOptions?: Partial<StateMachineOptions>);
    consume(text: string): string;
}

The StateMachine class is similar to the striptags function, but persists state across calls to consume() so that you may safely pass in a stream of text. For example:

const StateMachine = require("striptags").StateMachine;

// alternatively:
// import { StateMachine } from "striptags";

const instance = new StateMachine();

console.log(
  instance.consume("some text with <a") + instance.consume("tag>and more text")
);

Outputs:

some text with and more text

Options

  • allowedTags: Set<string> a set containing a list of tag names to allow (e.g. new Set(["tagname"])), default: new Set([]).
  • tagReplacementText: string a string to use as replacement text when a tag is found and not allowed, default: "".
  • encodePlaintextTagDelimiters: boolean true if < and > characters immediately followed by whitespace should be HTML encoded, default: true. This is safe to set to false if the output is expected to be used only as plaintext.

Current Tags

  • 4.0.0-alpha.4                                ...           alpha (4 years ago)
  • 3.2.0                                ...           latest (5 years ago)

20 Versions

  • 4.0.0-alpha.4                                ...           4 years ago
  • 4.0.0-alpha.3                                ...           4 years ago
  • 3.2.0                                ...           5 years ago
  • 4.0.0-alpha.2                                ...           6 years ago
  • 4.0.0-alpha.1                                ...           6 years ago
  • 4.0.0-alpha.0                                ...           6 years ago
  • 3.1.1                                ...           9 years ago
  • 3.1.0                                ...           9 years ago
  • 3.0.1                                ...           10 years ago
  • 3.0.0                                ...           10 years ago
  • 2.2.1                                ...           10 years ago
  • 2.2.0 [deprecated]           ...           10 years ago
  • 2.1.1                                ...           11 years ago
  • 2.1.0                                ...           11 years ago
  • 2.0.4                                ...           11 years ago
  • 2.0.3                                ...           11 years ago
  • 2.0.2                                ...           12 years ago
  • 2.0.1                                ...           12 years ago
  • 2.0.0                                ...           12 years ago
  • 1.0.0                                ...           12 years ago
Maintainers (1)
Downloads
Total 75
Today 1
This Week 11
This Month 52
Last Day 1
Last Week 7
Last Month 7
Dependencies (0)
None
Dev Dependencies (12)

© 2010 - cnpmjs.org x YWFE | Home | YWFE