UNPKG

yeying-tools

Version:

提供了格式化功能和转义还原HTML中的特殊字符

34 lines 836 B
// 1.定义转义html方法 function htmlEscape(htmlstr){ return htmlstr.replace(/<|>|"|&/g, (oldstr) => { switch(oldstr){ case '<': return "&lt;" case '>': return "&gt;" case '"': return "&quot;" case '&': return '&amp;' } }) } // 2.定义还原html的方法 function htmlUnEscape(htmlstr){ return htmlstr.replace(/&lt;|&gt;|&quot;|&amp;/g, (oldstr) => { switch(oldstr){ case '&lt;': return "<" case '&gt;': return ">" case '&quot;': return '"' case '&amp;': return '&' } }) } module.exports = { htmlEscape, htmlUnEscape }