Javascript String entityify()

Description

Javascript String entityify()


String.prototype.entityify = function () {
    var character = {
        '<': '$lt',
        '>': '&gt',
        '&': '&amp',
        '"': '&quot'
    };//from w ww . j  a  v  a  2s  .c o m
    //return the string.entityify method, which
    //returns the result of calling the replace method.
    //it's replaceValue function returns the result of
    //looking a character up in an object. This use of
    //an object ususally outerperforms switch statements.
    return function () {
        return this.replace(/[<>&"]/g, function (c) {
            return character[c];
        });
    };
}();

console.log("<&>".entityify());



PreviousNext

Related