Javascript将PascalCase转换为underscore_case

时间:2021-06-18 15:56:26

How can I convert PascalCase string into underscore_case string? I need conversion of dots to underscore as well.

如何将PascalCase字符串转换为underscore_case字符串?我也需要将点转换为下划线。

eg. convert

TypeOfData.AlphaBeta

into

type_of_data_alpha_beta

5 个解决方案

#1


53  

You could try the below steps.

您可以尝试以下步骤。

  • Capture all the uppercase letters and also match the preceding optional dot character.

    捕获所有大写字母,并匹配前面的可选点字符。

  • Then convert the captured uppercase letters to lowercase and then return back to replace function with an _ as preceding character. This will be achieved by using anonymous function in the replacement part.

    然后将捕获的大写字母转换为小写,然后返回以使用_作为前一个字符替换函数。这将通过在替换部件中使用匿名功能来实现。

  • This would replace the starting uppercase letter to _ + lowercase_letter.

    这会将起始大写字母替换为_ + lowercase_letter。

  • Finally removing the starting underscore will give you the desired output.

    最后删除起始下划线将为您提供所需的输出。

    var s = 'TypeOfData.AlphaBeta';
    console.log(s.replace(/(?:^|\.?)([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));
    

OR

var s = 'TypeOfData.AlphaBeta';
alert(s.replace(/\.?([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));

any way to stop it for when a whole word is in uppercase. eg. MotorRPM into motor_rpm instead of motor_r_p_m? or BatteryAAA into battery_aaa instead of battery_a_a_a?

当整个单词都是大写时,任何阻止它的方法。例如。 MotorRPM进入motor_rpm而不是motor_r_p_m?或者BatteryAAA进入battery_aaa而不是battery_a_a_a?

var s = 'MotorRMP';
alert(s.replace(/\.?([A-Z]+)/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));

#2


27  

Alternatively using lodash:

或者使用lodash:

lodash.snakeCase(str);

Example:

_.snakeCase('TypeOfData.AlphaBeta');
// ➜ 'type_of_data_alpha_beta'

Lodash is a fine library to give shortcut to many everyday js tasks.There are many other similar string manipulation functions such as camelCase, kebabCase etc.

Lodash是一个很好的库,可以提供许多日常js任务的快捷方式。还有许多其他类似的字符串操作函数,如camelCase,kebabCase等。

#3


26  

str.split(/(?=[A-Z])/).join('_').toLowerCase();

u're welcome

var s1 = 'someTextHere';
var s2 = 'SomeTextHere';

var o1 = s1.split(/(?=[A-Z])/).join('_').toLowerCase();
var o2 = s2.split(/(?=[A-Z])/).join('_').toLowerCase();

console.log(o1);
console.log(o2);

#4


1  

This will get you pretty far: https://github.com/domchristie/humps

这将让你走得很远:https://github.com/domchristie/humps

You will probably have to use regex replace to replace the "." with an underscore.

您可能必须使用正则表达式替换来替换“。”用下划线。

#5


1  

function toCamelCase(s) {
  // remove all characters that should not be in a variable name
  // as well underscores an numbers from the beginning of the string
  s = s.replace(/([^a-zA-Z0-9_\- ])|^[_0-9]+/g, "").trim().toLowerCase();

  // uppercase letters preceeded by a hyphen or a space
  s = s.replace(/([ -]+)([a-zA-Z0-9])/g, function(a,b,c) {
    return c.toUpperCase();
  });

  // uppercase letters following numbers
  s = s.replace(/([0-9]+)([a-zA-Z])/g, function(a,b,c) {
    return b + c.toUpperCase();
  });

  return s;
}

Try this function, hope it helps.

尝试这个功能,希望它有所帮助。

#1


53  

You could try the below steps.

您可以尝试以下步骤。

  • Capture all the uppercase letters and also match the preceding optional dot character.

    捕获所有大写字母,并匹配前面的可选点字符。

  • Then convert the captured uppercase letters to lowercase and then return back to replace function with an _ as preceding character. This will be achieved by using anonymous function in the replacement part.

    然后将捕获的大写字母转换为小写,然后返回以使用_作为前一个字符替换函数。这将通过在替换部件中使用匿名功能来实现。

  • This would replace the starting uppercase letter to _ + lowercase_letter.

    这会将起始大写字母替换为_ + lowercase_letter。

  • Finally removing the starting underscore will give you the desired output.

    最后删除起始下划线将为您提供所需的输出。

    var s = 'TypeOfData.AlphaBeta';
    console.log(s.replace(/(?:^|\.?)([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));
    

OR

var s = 'TypeOfData.AlphaBeta';
alert(s.replace(/\.?([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));

any way to stop it for when a whole word is in uppercase. eg. MotorRPM into motor_rpm instead of motor_r_p_m? or BatteryAAA into battery_aaa instead of battery_a_a_a?

当整个单词都是大写时,任何阻止它的方法。例如。 MotorRPM进入motor_rpm而不是motor_r_p_m?或者BatteryAAA进入battery_aaa而不是battery_a_a_a?

var s = 'MotorRMP';
alert(s.replace(/\.?([A-Z]+)/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));

#2


27  

Alternatively using lodash:

或者使用lodash:

lodash.snakeCase(str);

Example:

_.snakeCase('TypeOfData.AlphaBeta');
// ➜ 'type_of_data_alpha_beta'

Lodash is a fine library to give shortcut to many everyday js tasks.There are many other similar string manipulation functions such as camelCase, kebabCase etc.

Lodash是一个很好的库,可以提供许多日常js任务的快捷方式。还有许多其他类似的字符串操作函数,如camelCase,kebabCase等。

#3


26  

str.split(/(?=[A-Z])/).join('_').toLowerCase();

u're welcome

var s1 = 'someTextHere';
var s2 = 'SomeTextHere';

var o1 = s1.split(/(?=[A-Z])/).join('_').toLowerCase();
var o2 = s2.split(/(?=[A-Z])/).join('_').toLowerCase();

console.log(o1);
console.log(o2);

#4


1  

This will get you pretty far: https://github.com/domchristie/humps

这将让你走得很远:https://github.com/domchristie/humps

You will probably have to use regex replace to replace the "." with an underscore.

您可能必须使用正则表达式替换来替换“。”用下划线。

#5


1  

function toCamelCase(s) {
  // remove all characters that should not be in a variable name
  // as well underscores an numbers from the beginning of the string
  s = s.replace(/([^a-zA-Z0-9_\- ])|^[_0-9]+/g, "").trim().toLowerCase();

  // uppercase letters preceeded by a hyphen or a space
  s = s.replace(/([ -]+)([a-zA-Z0-9])/g, function(a,b,c) {
    return c.toUpperCase();
  });

  // uppercase letters following numbers
  s = s.replace(/([0-9]+)([a-zA-Z])/g, function(a,b,c) {
    return b + c.toUpperCase();
  });

  return s;
}

Try this function, hope it helps.

尝试这个功能,希望它有所帮助。