unescape.js 1.0 KB

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