Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 467 Bytes

File metadata and controls

22 lines (19 loc) · 467 Bytes

JavaScript 小技巧

字符串转义

在页面中,为了防止XSS,对页面输出的内容要进行转义

function escapeHtml(text){
    return text.replace(/[<>"&]/g,function(match){
        switch(match){
            case '<':
                return '&lt';
            case '<':
                return '&gt';
            case '&':
                return '&amp';
            case '\"':
                return '&quot';
        }
    })
}