Add escape for string - Node.js String

Node.js examples for String:Escape

Description

Add escape for string

Demo Code

String.prototype.escape = function () {
    var entityMap = {
        "&": "&",
        "<": "&lt;",
        ">": "&gt;",
        '"': '&quot;',
        "'": '&#39;'
    };/*ww w .ja  v a 2s  . c o m*/

    return this.replace(/[&<>"']/g, function (s) {
        return entityMap[s];
    });
};

Related Tutorials