Last active
January 10, 2018 17:55
-
-
Save davidicus/d59a4b3bc167aab620082c61c5f67924 to your computer and use it in GitHub Desktop.
TitleCase a string function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const toTitleCase = (str) => { | |
return ( | |
str.replace(/([^\W]+[^\s-]*) */g, | |
txt => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase() | |
)) | |
}; | |
const string = 'this-is my string-yo'; | |
toTitleCase(string); | |
//outputs => "This-Is My String-Yo" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works to capitalize the first letter of every word, even ones separated by (-) hyphen.