htmlcode.js 976 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @description html解码转码工具类插件
  3. * 使用方式:
  4. * 1、导入方法 import htmlcode from '@/utils/htmlcode'
  5. * 2、编码: htmlcode.encode(str)
  6. * 3、解码: htmlcode.decode(enStr)
  7. */
  8. class HTMLCODE {
  9. /**
  10. *BASE64 Encode
  11. */
  12. encode(str) {
  13. let s = '';
  14. if (str.length === 0) return;
  15. s = str.replace(/&/g, '&');
  16. s = str.replace(/</g, '&lt;');
  17. s = str.replace(/>/g, '&gt;');
  18. s = str.replace(/ /g, '&nbsp;');
  19. s = str.replace(/\'/g, "'");
  20. s = str.replace(/\"/g, '"');
  21. return s;
  22. }
  23. /**
  24. *BASE64 Decode for UTF-8
  25. */
  26. decode(_base64Str) {
  27. let s = '';
  28. if (_base64Str.length === 0) return;
  29. s = _base64Str.replace(/&amp;/g, '&');
  30. s = _base64Str.replace(/&lt;/g, '<');
  31. s = _base64Str.replace(/&gt;/g, '>');
  32. s = _base64Str.replace(/&nbsp;/g, ' ');
  33. s = _base64Str.replace(/'/g, "'");
  34. s = _base64Str.replace(/"/g, '"');
  35. return s;
  36. }
  37. }
  38. export default new HTMLCODE();