js将连接符命名和驼峰命名互转
js 将用连接符连接的英文字符串转换成驼峰命名
1 // transformStr3('edsp-log-ui-test') // "edspLogUiTest" 2 function transformStr3(str) { 3 var re = /-(\w)/g; 4 return str.replace(re, function ($0, $1) { 5 return $1.toUpperCase(); 6 }); 7 }
将驼峰命名转成连接符命名
1 // camelCaseToHyphen('edspLogUiTest') //"edsp-log-ui-test" 2 function camelCaseToHyphen(str) { 3 return str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); 4 }
// 'edsp-log-ui-test') --> "EdspLogUiTest" name.replace(/-(\w)/g, ($0, $1) => $1.toUpperCase()).replace(/^\S/, s => s.toUpperCase())
const upperCamelCase = require('uppercamelcase'); upperCamelCase('foo-bar'); //=> FooBar