node07_自定义一个html转义模块
在日常开发的过程中除了转换时间之外,我们可能还会遇到需要将html代码转义成文本,以及将文本转义成html的需求,下面我们来自定义一个转义的模块。
具体的思路就是使用正则表达式,找到对应的要转义的字符进行转义即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | function htmlEsplace(htmlstr) { //定义转义html方法 return htmlstr.replace(/<|>| "|&/g, (match) => { switch (match) { case '<': return '<' case '>': return '>' case '" ': return '"' case '&' : return '&' } }) } function reductionHtml(str){ //还原HTML的方法 return str.replace(/<|>|"|&/g,(match)=>{ switch (match){ case '<' : return '<' case '>' : return '>' case '"' : return '"' case '&' : return '&' } }) } module.exports={ reductionHtml, htmlEsplace } |
引入并使用
本文作者:SadicZhou
本文链接:https://www.cnblogs.com/SadicZhou/p/16882714.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步