How could I convert this to PascalCase
and to camelCase
?
var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
How could I convert this to PascalCase
and to camelCase
?
var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
A fully ES5 compatible way to do this is, to find all the dashes that are followed by an alphanumeric character using this simple regex /-\w/g
. Then just remove the dash and uppercase the character.
The same can be done for pascal case just by also checking for the first character in the string using ^\w|-\w
. The rest is the same.
Here are a couple of examples:
console.log(toCamelCase("welcome-to-a-New-day"));
console.log(toPascalCase("welcome-to-a-New-day"));
console.log(toCamelCase("bsd-asd-csd"));
console.log(toPascalCase("bsd-asd-csd"));
function toCamelCase(text) {
return text.replace(/-\w/g, clearAndUpper);
}
function toPascalCase(text) {
return text.replace(/(^\w|-\w)/g, clearAndUpper);
}
function clearAndUpper(text) {
return text.replace(/-/, "").toUpperCase();
}
You do that using replace
and RegExp
let string = "welcome-to-a-New-day"
function toPascalCase(str){
let arr = str.split('-');
let last = arr[arr.length - 1];
str = str.replace(/-\w/g,(x) => `${x[1].toUpperCase()}`)
str = str[0].toUpperCase() + str.substring(1,str.length - last.length) + last.toUpperCase();
return str;
}
function toCamelCase(str){
return str.replace(/-\w/g,(x) => `${x[1].toUpperCase()}`)
}
console.log(toPascalCase(string))
console.log(toCamelCase(string))
There you go
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getPascalFromSnake(input){
return input.split("-").map(capitalizeFirstLetter).join("")
}
function getCamelFromSnake(input){
var toReturn = getPascalFromSnake(input);
return toReturn.charAt(0).toLowerCase() + toReturn.slice(1);
}
const myString = 'welcome-to-a-New-day';
function capitalize(string) {
// take first character, uppercase it
// add the rest of the string
return string.charAt(0).toUpperCase() + string.slice(1);
}
function pascalize (string) {
// splitting words by dash
const words = string.split('-')
// use capitalize function to capitalize every word
const capitalized = words.map(word => capitalize(word))
// glue up words with .join()
return capitalized.join('')
}
function camelize (string) {
// splitting words by dash
const words = string.split('-')
// use capitalize function to capitalize every but first word
const capitalized = words.map((word, index) => {
if (index === 0) return word
return capitalize(word)
})
// glue up words with .join()
return capitalized.join('')
}
console.log(pascalize(myString))
console.log(camelize(myString))
Here is a way to do it with string.replace()
and a regex:
const camelCase = str => str.replace(/\s*-\s*\w/g, parts => parts[parts.length-1].toUpperCase());
const pascalCase = str => camelCase(str).replace(/^\w/, s => s.toUpperCase());
const cases = str => ({ pascal: pascalCase(str), camel: camelCase(str) });
console.log(cases('welcome-to-a-New-day'));
console.log(cases('welcome -to-a- New - day'));
str.replace(/(^\w)|(-\w)/g, (gr0, gr1, gr2) => gr1?.toUpperCase() ?? gr2[gr2.length-1].toUpperCase())
–
Xerosere function getCase(str) {
let arr= str.split("-");
return arr.reduce((acc, word,i)=>{
let lower = word.toLowerCase(),
cap = lower.slice(0,1).toUpperCase() + lower.slice(1);
return {
camel: i === 0 ? lower : acc.camel + cap,
pascal: acc.pascal + cap
}
},{camel:"",pascal:""})
}
@Nick's
answer, just in one function:
function toPascalCase(text) {
return text.replace(/(^\w|-\w)/g, (text) => text.replace(/-/, "").toUpperCase());
}
First put a dash in front of the string, and then replace every -\w
with just the uppercase character, omitting the dash:
function pascalCase(str) {
return `-${str}`.replace(/-\w/g, (c) => c[1].toUpperCase())
}
© 2022 - 2025 — McMap. All rights reserved.
{pascal: WelcomeToANewDAY,
Why isDAY
notDay
? – Patristic