📅  最后修改于: 2022-03-11 15:01:28.756000             🧑  作者: Mango
text.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Explanation:
//
// ^\w : first character of the string
// | : or
// \s\w : first character after whitespace
// (^\w|\s\w) Capture the pattern.
// g Flag: Match all occurrences.
// Example usage:
// Create a reusable function:
const toTitleCase = str => str.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Call the function:
const myStringInTitleCase = toTitleCase(myString);