how to escape xml entities in javascript?

前端 未结 10 1181
醉话见心
醉话见心 2020-11-27 14:51

In JavaScript (server side nodejs) I\'m writing a program which generates xml as output.

I am building the xml by concatenating a string:

str += \'&l         


        
10条回答
  •  一生所求
    2020-11-27 15:32

    This might be a bit more efficient with the same outcome:

    function escapeXml(unsafe) {
        return unsafe.replace(/[<>&'"]/g, function (c) {
            switch (c) {
                case '<': return '<';
                case '>': return '>';
                case '&': return '&';
                case '\'': return ''';
                case '"': return '"';
            }
        });
    }
    

提交回复
热议问题