String truncate - Node.js String

Node.js examples for String:Strip

Description

String truncate

Demo Code


String.prototype.truncate = function(len){
    len = (len!=null) ? len : 25;/* w  w  w . j  a  v  a  2s .c om*/
    var re = this.match(RegExp("^.{0,"+len+"}[\S]*"));
    var l = re[0].length;
    re = re[0].replace(/\s$/,'');
    if(l < this.length) {
        re = re + "&hellip;";
    }
    return re;
};

Related Tutorials