Problem
- Complete the solution so that the function will break up camel casing, using a space between words.
- 단어 사이의 공백을 사용해서 낙타대문자(camelCase)를 분해해서 반환한다.
Solution 01
function breakCamelCase(str) {
let arr = str.split('');
let result = '';
arr.forEach(i => {
if (i === i.toUpperCase()) {
result += ' ' + i;
} else {
result += i;
}
});
return result;
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 02
function breakCamelCase(str) {
let arr = str.split('');
let result = [];
arr.forEach(i => {
if (i === i.toUpperCase()) {
result.push(' ');
}
result.push(i);
});
return result.join('');
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 03
function breakCamelCase(str) {
return [...str].map(i => {
return i === i.toUpperCase() ? ` ${i}` : i;
}).join('');
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 04
function breakCamelCase(str) {
return str.replace(/[A-Z]/g, function(i) {
return ' ' + i;
});
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 05
function breakCamelCase(str) {
return str.replace(/(?=[A-Z])/g, ' ');
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 06
function breakCamelCase(str) {
return str.replace(/([a-z])([A-Z])/g, '$1 $2');
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number
Solution 07
function breakCamelCase(str) {
return str.replace(/[A-Z]/g, ' $&');
}
breakCamelCase('camelCasing'); // camel Casing
breakCamelCase('createPhoneNumber'); // create Phone Number