node package manager

escape-goat

escape-goat

<img src="logo.jpg" width="1280" alt="escape-goat">

Escape a string for use in HTML or the inverse

Build Status

Install

$ npm install escape-goat

Usage

const escapeGoat = require('escape-goat');
 
escapeGoat.escape('🦄 & 🐐');
//=> '🦄 &amp; 🐐' 
 
escapeGoat.unescape('🦄 &amp; 🐐');
//=> '🦄 & 🐐' 
 
escapeGoat.escape('Hello <em>World</em>');
//=> 'Hello &lt;em&gt;World&lt;/em&gt;' 
 
const url = 'https://sindresorhus.com?x="🦄"';
escapeGoat.escapeTag`<a href="${url}">Unicorn</a>`;
//=> '<a href="https://sindresorhus.com?x=&quot;🦄&quot;">Unicorn</a>' 

API

escapeGoat.escape(input)

Escapes the following characters in the given input string: & < > " '

escapeGoat.unescape(input)

Unescapes the following HTML entities in the given input string: &amp; &lt; &gt; &quot; &#39;

escapeGoat.escapeTag

Tagged template literal that escapes interpolated values.

escapeGoat.unescapeTag

Tagged template literal that unescapes interpolated values.

Tip

Ensure you always quote your HTML attributes to prevent possible XSS.

FAQ

Why yet another HTML escaping package?

I couldn't find one I liked that was tiny, well-tested, and had both .escape() and .unescape().

License

MIT © Sindre Sorhus