天天看點

js前端特殊字元處理

 function escapeHtmlEntities (str) {

    var escapes = {

        '\\' : '\\',

        '"' : '"',

        '/' : '/',

        't' : '\t',

        'n' : '\n',

        'r' : '\r',

        'f' : '\f',

        'b' : '\b',

        '<' : '<',

        '>' : '>'

    };

    return str.replace(/\\(?:u(.{4})|([^u]))/g, function(a, b, c) {

        return b ? String.fromCharCode(parseInt(b, 16)) : escapes[c];

    });

}