Reputation: 2588
I am trying to capitalize the String after removing all dashes in between.
so this i-am-string
would become I am string
.
This is what I tried, but it does capitalize, but I don't know how to remove dashes and capitalize.
function tweakFunction (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
Thanks
Upvotes: 0
Views: 3356
Reputation: 56
//split into array of multiple words using the dash, iterate through each word to make whatever change you want, then join.
const formatSlug = (string) => {
return string
.split('-')
.map((str) => str.charAt(0).toUpperCase() + str.slice(1))
.join(' ');
};
Upvotes: 0
Reputation: 1
/* Capitalize the first letter of the string and the rest of it to be Lowercase */
function capitalize(word){
return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase()
}
console.log(capitalize("john")); //John
console.log(capitalize("BRAVO")); //Bravo
console.log(capitalize("BLAne")); //Blane
Upvotes: 0
Reputation: 70732
function tweakFunction(str) {
str = str.replace(/-/g, ' ');
return str.charAt(0).toUpperCase() + str.slice(1);
}
console.log(tweakFunction('i-am-string')); //=> "I am string"
Upvotes: 2
Reputation: 94101
You could use a couple regex, like in the PHP version you previously posted:
var result = str
.replace(/-/g, ' ')
.replace(/^./, function(x){return x.toUpperCase()})
Upvotes: 1