Nodejs String to Underscore underscore()

Here you can find the source of underscore()

Method Source Code

/**//from  w ww .j a va  2 s . com
 *  String#underscore() -> String
 *
 *  Converts a camelized string into a series of words separated by an
 *  underscore (`_`).
 *
 *  ##### Example
 *
 *      'borderBottomWidth'.underscore();
 *      // -> 'border_bottom_width'
 *
 *  ##### Note
 *
 *  Used in conjunction with [[String#dasherize]], [[String#underscore]]
 *  converts a DOM style into its CSS equivalent.
 *
 *      'borderBottomWidth'.underscore().dasherize();
 *      // -> 'border-bottom-width'
**/
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();
};

Related

  1. toUnderscore()
    String.prototype.toUnderscore = function() {
      return this.replace(/(^[A-Z])/, function(match) {
        return match.toLowerCase();
      }).replace(/([A-Z])/g, function(match){
        return "_" + match.toLowerCase();
      });
    };
    
  2. toUnderscore()
    String.prototype.toUnderscore = function() {
      var str = this.replace(/[A-Z]/g, function(s) {
        return "_" + s.toLowerCase();
      });
      while (str.charAt(0) === '_') {
        str = str.slice(1);
      return str;
    };
    ...
    
  3. toUnderscore()
    String.prototype.toUnderscore = function() {
      var str = this;
      var modified = '';
      for (var i = 0; i < str.length; i++) {
        if (isLower(str[i])) {
          modified += str[i];
        } else {
          if (str.length > i-1) {
            if (isLower(str[i-1])) {
    ...
    
  4. underscore()
    String.prototype.underscore = function() {
      return this.replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
    };
    
  5. 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();
    };
    
  6. underscore()
    String.prototype.underscore = function() {
      return this.trim().toLowerCase().replace(/[\s]+/g, '_');
    
  7. 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();
    };
    
  8. underscorize()
    String.prototype.underscorize = function() {
      return this.replace(/([A-Z])/g, function($1) {
        return "_" + $1.toLowerCase();
      }).replace(/^_/g, "");
    };