unescapeHtml.js 922 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export function unescape(html) {
  2. return html
  3. .replace(html ? /&(?!#?\w+;)/g : /&/g, '')
  4. .replace(/&lt;/g, "<")
  5. .replace(/&gt;/g, ">")
  6. .replace(/&quot;/g, "\"")
  7. .replace(/&#39;/g, "\'")
  8. .replace(/nbsp;/g,"\u00a0")
  9. .replace(/&amp;/g,"")
  10. }
  11. /**
  12. * 反转义html
  13. * @param html
  14. * @returns {*}
  15. */
  16. export function unescapeHtml(html) {
  17. if(!html) return html;
  18. return html
  19. .replace(html ? /&(?!#?\w+;)/g : /&/g, '')
  20. .replace(/&lt;/g, "<")
  21. .replace(/&gt;/g, ">")
  22. .replace(/&quot;/g, "\"")
  23. .replace(/&#39;/g, "\'")
  24. .replace(/nbsp;/g,"\u00a0")
  25. .replace(/&amp;/g,"")
  26. }
  27. /**
  28. * 转义html
  29. * @param html
  30. * @returns {*}
  31. */
  32. export function escapeHtml(html) {
  33. if(!html) return html;
  34. return html
  35. .replace(/&/g, '&amp;')
  36. .replace(/</g, '&lt;')
  37. .replace(/"/g, '&quot;')
  38. .replace(/'/g, "&#39;")
  39. .replace(/>/g, '&gt;')
  40. .replace(/\u00a0/g,"nbsp;")
  41. }