unescapeHTML.js 593 B

123456789101112131415161718
  1. var makeString = require('./helper/makeString');
  2. var htmlEntities = require('./helper/htmlEntities');
  3. module.exports = function unescapeHTML(str) {
  4. return makeString(str).replace(/\&([^;]+);/g, function(entity, entityCode) {
  5. var match;
  6. if (entityCode in htmlEntities) {
  7. return htmlEntities[entityCode];
  8. } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
  9. return String.fromCharCode(parseInt(match[1], 16));
  10. } else if (match = entityCode.match(/^#(\d+)$/)) {
  11. return String.fromCharCode(~~match[1]);
  12. } else {
  13. return entity;
  14. }
  15. });
  16. };