text.replace(/(^w|sw)/g, m => m.toUpperCase());
// Explanation:
//
// ^w : first character of the string
// | : or
// sw : first character after whitespace
// (^w|sw) Capture the pattern.
// g Flag: Match all occurrences.
// Example usage:
// Create a reusable function:
const toTitleCase = str => str.replace(/(^w|sw)/g, m => m.toUpperCase());
// Call the function:
const myStringInTitleCase = toTitleCase(myString);