xxxxxxxxxx
String.prototype.toCamelCase = function () {
let STR = this.toLowerCase()
.trim()
.split(/[ -_]/g)
.map(word => word.replace(word[0], word[0].toString().toUpperCase()))
.join('');
return STR.replace(STR[0], STR[0].toLowerCase());
};
xxxxxxxxxx
function toCamelCase(string) {
string = string.toLowerCase()
return string.replace(/(?:_)([a-z])/g, (match, group1) => group1.toUpperCase())
}
toCamelCase('THIS_IS_A_TEST') //thisIsATest
xxxxxxxxxx
"thisStringIsGood"
// insert a space before all caps
.replace(/([A-Z])/g, ' $1')
// uppercase the first character
.replace(/^./, function(str){ return str.toUpperCase(); })
xxxxxxxxxx
const camelCase = (string) => {
function camelize(str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
})
.replace(/\s+/g, "");
}
const newText = camelize(string);
return newText
};
Convert Snake Case to Camel Case
xxxxxxxxxx
let snakeCaseToCamelCase = function (snakeCaseWord) {
let wordArray = snakeCaseWord.toLowerCase().split("_");
let newWordArray = [];
for (let word of wordArray) {
let capWord = word[0].toUpperCase() + word.substring(1);
newWordArray.push(capWord);
}
return newWordArray.join("");
}
xxxxxxxxxx
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
xxxxxxxxxx
function toCamelCase(str) {
return str
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
}