Q:

Change case with RegEx where there may be numbers in the string

I have this function to convert strings to title case,
export function toTitleCase(text) {
  const result = text.replace(/([A-Z])/g, ' $1');
  return result.charAt(0).toUpperCase() + result.slice(1);
}

With camelCase the result is Camel Case. 
The result is correct. 
With camel23Case the result is Camel23 Case. 
I would like the result to be Camel 23 Case
With camelCASE the result is Camel C A S E.
I would like the result to be Camel CASE. 

A:

Look for sequences of uppercase letters or numbers, and add a space before them.  Then replace the 1st lowercase letters with uppercase versions:

const toTitleCase = (text) => text
  .replace(/([A-Z]+|[0-9]+)/g, ' $1')
  .replace(/\b([a-z])/g, (m) => m.toUpperCase());

console.log(toTitleCase('camelCase'));
console.log(toTitleCase('camel23Case'));
console.log(toTitleCase('camelCASE'));

