灵心如玉,守一生无惧|

SadicZhou

园龄:3年2个月粉丝:7关注:4

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 中国大陆许可协议进行许可。

posted @   SadicZhou  阅读(56)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 尚好的青春 孙燕姿
  2. 2 孙燕姿
  3. 3 克卜勒 孙燕姿
- 孙燕姿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.