Nodejs String to Underscore underscorize()

Here you can find the source of underscorize()

Method Source Code

String.prototype.underscorize = function() {
  return this.replace(/([A-Z])/g, function($1) {
    return "_" + $1.toLowerCase();
  }).replace(/^_/g, "");
};

Related

  1. underscore()
    String.prototype.underscore = function() {
      return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
    };
    
  2. underscore()
    String.prototype.underscore = function() {
      return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
    };
    
  3. underscore()
    String.prototype.underscore = function() {
      return this
        .replace(/::/g, '/')
        .replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
        .replace(/([a-z\d])([A-Z])/g, '$1_$2')
        .replace(/-/g, '_')
        .toLowerCase();
    };
    
  4. underscore()
    String.prototype.underscore = function() {
      return this.trim().toLowerCase().replace(/[\s]+/g, '_');
    
  5. underscore()
    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();
    };