var text = "welcome-to-a-New-day";
toPascalCase(text); // "WelcomeToANewDAY"
toCamelCase(text); // "WelcomeToANewDAY"
如何将其转换为PascalCase
和camelCase
?
答案 0 :(得分:2)
一种完全与ES5兼容的方法是使用此简单的正则表达式/-\w/g
查找所有短划线,后跟字母数字字符。然后只需删除破折号并将字符大写即可。
对于pascal情况也可以做到这一点,只需使用^\w|-\w
检查字符串中的第一个字符即可。其余的都一样。
以下是几个示例:
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();
}
答案 1 :(得分:1)
您可以使用replace
和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))
答案 2 :(得分:0)
这是使用string.replace()
和正则表达式的一种方法:
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'));
答案 3 :(得分:0)
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))
答案 4 :(得分:0)
去那里
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);
}
答案 5 :(得分:0)
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:""})
}