[SOLVED] Convert camelCaseText to Title Case Text

Issue

How can I convert a string either like ‘helloThere’ or ‘HelloThere’ to ‘Hello There’ in JavaScript?

Solution

const text = 'helloThereMister';
const result = text.replace(/([A-Z])/g, " $1");
const finalResult = result.charAt(0).toUpperCase() + result.slice(1);
console.log(finalResult);

capitalize the first letter – as an example. Note the space in " $1".


Of course, in case the first letter is already capital – you would have a spare space to remove.

Answered By – ZenMaster

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *