UNPKG

yf-node-utils

Version:

格式化日期 html特殊字符转换

38 lines (36 loc) 944 B
// 将html字符串转换成包含转义字符的字符串 function htmlEscape(htmlStr){ return htmlStr.replace(/<|>|"|&/g,(match)=>{ switch (match){ case '<': return '&lt;' case '>': return '&gt;' case '"': return '&quot;' case '&': return '&amp;' } }) } // 将包含转义字符的字符串转换为html字符串 function htmlUnEscape(str){ return str.replace(/&lt;|&gt;|&quot;|&amp;/g,(match)=>{ switch (match){ case '&lt;': return '<' case '&gt;': return '>' case '&quot;': return '"' case '&amp;': return '&' } }) } // 将这两个函数对外暴露 module.exports = { // 等价于 "htmlEscape":htmlEscape, "htmlUnEscape":htmlUnEscape }