난 정규식 전문가는 한 줄에 같은 것을 할 수있을 거라고 생각했지만 , 개인적으로 나는 이런 것을 할 것입니다.
function humanize(str) {
return str.trim().split(/\s+/).map(function(str) {
return str.replace(/_/g, ' ').replace(/\s+/, ' ').trim();
}).join(' ').toLowerCase().replace(/^./, function(m) {
return m.toUpperCase();
});
}
테스트 :
[
' this is a test',
'foo Bar Baz',
'foo_bar',
'foo-bar',
'fooBarBaz',
'_fooBarBaz____',
'_alpha',
'hello_ _world, how are________you? '
].map(humanize);
/* Result:
[
"This is a test",
"Foo bar baz",
"Foo bar",
"Foo-bar",
"Foobarbaz",
"Foobarbaz",
"Alpha",
"Hello world, how are you?"
]
*/
CamelCase 인 경우 혼자 남겨 두어야합니까? 대시가있는 단어와 동일한 원리입니다. 그러나 첫 번째 문자는 항상 대문자이어야합니다. 나는 편집을했다. –