String underscore - Node.js String

Node.js examples for String:Format

Description

String underscore

Demo Code


String.prototype.capitalize = function () {
    var str = this.toLowerCase();
    return str.charAt(0).toUpperCase() + str.substr(1);
};

String.prototype.camelize = function (lower_case_and_underscored_word) {
    var parts = this.split('_'), str = "";
    if (lower_case_and_underscored_word === 'lower') {
        str = parts.shift();//  w  w  w  .  java  2  s .  c o  m
    }
    for (var i = 0, len = parts.length; i < len; i++) {
        str += parts[i].capitalize();
    }
    return str;
};

String.prototype.underscore = function () {
    var str = this;
    str = str.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2');
    str = str.replace(/([a-z\d])([A-Z])/g, '$1_$2');
    str = str.replace(/\-/g, '_');
    return str.toLowerCase();
};

String.prototype.dasherize = function () {
    return this.replace(/_/g, '-');
};

Related Tutorials