unescape.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. var baseToString = require('../internal/baseToString'),
  2. unescapeHtmlChar = require('../internal/unescapeHtmlChar');
  3. /** Used to match HTML entities and HTML characters. */
  4. var reEscapedHtml = /&(?:amp|lt|gt|quot|#39|#96);/g,
  5. reHasEscapedHtml = RegExp(reEscapedHtml.source);
  6. /**
  7. * The inverse of `_.escape`; this method converts the HTML entities
  8. * `&`, `<`, `>`, `"`, `'`, and ``` in `string` to their
  9. * corresponding characters.
  10. *
  11. * **Note:** No other HTML entities are unescaped. To unescape additional HTML
  12. * entities use a third-party library like [_he_](https://mths.be/he).
  13. *
  14. * @static
  15. * @memberOf _
  16. * @category String
  17. * @param {string} [string=''] The string to unescape.
  18. * @returns {string} Returns the unescaped string.
  19. * @example
  20. *
  21. * _.unescape('fred, barney, & pebbles');
  22. * // => 'fred, barney, & pebbles'
  23. */
  24. function unescape(string) {
  25. string = baseToString(string);
  26. return (string && reHasEscapedHtml.test(string))
  27. ? string.replace(reEscapedHtml, unescapeHtmlChar)
  28. : string;
  29. }
  30. module.exports = unescape;