驼峰命名法字符串转换
要将字符串转换为驼峰命名法,请执行以下步骤:
- 打开终端/SSH 并输入
node 以开始练习编码。
- 使用
String.prototype.match() 和适当的正则表达式将字符串拆分为单词。
- 使用
Array.prototype.map()、Array.prototype.slice()、Array.prototype.join()、String.prototype.toLowerCase() 和 String.prototype.toUpperCase() 来组合单词并将每个单词的首字母大写。
- 使用下面显示的
toCamelCase 函数进行转换:
const toCamelCase = (str) => {
const words =
str &&
str.match(
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g
);
const capitalizedWords =
words &&
words.map(
(word) => word.slice(0, 1).toUpperCase() + word.slice(1).toLowerCase()
);
const camelCaseString = capitalizedWords && capitalizedWords.join("");
return (
camelCaseString &&
camelCaseString.slice(0, 1).toLowerCase() + camelCaseString.slice(1)
);
};
以下是一些使用 toCamelCase 函数的示例:
toCamelCase("some_database_field_name"); //'someDatabaseFieldName'
toCamelCase("Some label that needs to be camelized");
//'someLabelThatNeedsToBeCamelized'
toCamelCase("some-javascript-property"); //'someJavascriptProperty'
toCamelCase("some-mixed_string with spaces_underscores-and-hyphens");
//'someMixedStringWithSpacesUnderscoresAndHyphens'