The lodash.unescape function converts HTML entities (&, <, >, ", ', and `) in strings back to their corresponding characters. This function is the inverse of lodash's escape function and is commonly used when displaying HTML content that has been previously escaped for security purposes.
npm install lodashimport { unescape } from "lodash";For CommonJS:
const { unescape } = require("lodash");Alternative import from the main lodash object:
import _ from "lodash";
// Use as _.unescape()import { unescape } from "lodash";
// Basic HTML entity unescaping
const escaped = 'fred, barney, & pebbles';
const unescaped = unescape(escaped);
console.log(unescaped); // => 'fred, barney, & pebbles'
// Multiple entity types
const multipleEntities = '<div>"Hello World"</div>';
const result = unescape(multipleEntities);
console.log(result); // => '<div>"Hello World"</div>'
// Handles mixed content
const mixed = 'Price: < $100 & > $50';
const cleanMixed = unescape(mixed);
console.log(cleanMixed); // => 'Price: < $100 & > $50'Converts HTML entities to their corresponding characters in a string.
/**
* The inverse of `_.escape`; this method converts the HTML entities
* `&`, `<`, `>`, `"`, `'`, and ``` in `string` to their
* corresponding characters.
*
* @param {string} [string=''] The string to unescape.
* @returns {string} Returns the unescaped string.
*/
function unescape(string)Supported HTML Entities:
& → & (ampersand)< → < (less than)> → > (greater than)" → " (double quote)' → ' (single quote/apostrophe)` → ` (backtick)Usage Examples:
import { unescape } from "lodash";
// Basic unescaping
unescape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
// HTML tag simulation
unescape('<script>alert('hello');</script>');
// => "<script>alert('hello');</script>"
// Empty and null handling
unescape(''); // => ''
unescape(null); // => ''
unescape(undefined); // => ''
// Non-string input gets converted to string first
unescape(123); // => '123'
unescape(true); // => 'true'
// Strings without entities are returned unchanged
unescape('hello world'); // => 'hello world'
// Mixed escaped and unescaped content
unescape('Tom & Jerry < Mickey Mouse');
// => 'Tom & Jerry < Mickey Mouse'The unescape function is designed to handle various input types gracefully:
No exceptions are thrown under normal circumstances.