Skip to content

Instantly share code, notes, and snippets.

@davidicus
Last active January 10, 2018 17:55
Show Gist options
  • Save davidicus/d59a4b3bc167aab620082c61c5f67924 to your computer and use it in GitHub Desktop.
Save davidicus/d59a4b3bc167aab620082c61c5f67924 to your computer and use it in GitHub Desktop.
TitleCase a string function
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"
@davidicus
Copy link
Author

Works to capitalize the first letter of every word, even ones separated by (-) hyphen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment