0

I have the following string:

A new way's to get home

Output need to be:

A New Way's To Get Home

I try with this code:

var string = "A new way's to get home";

string = string.replace(/\(\d+\)/g, '').trim().replace(': ', ' - ').replace(/\b[a-z]/g,function(f){return f.toUpperCase();});

alert(string);

But i get this:

A New Way'S To Get Home

I need to get all string at begginning to uppercase but here the problem is Way'S because it contains ' and this regex above need to return 's not 'S how i need to rewrite this regex to return correct value?

John
  • 1,521
  • 3
  • 15
  • 31
  • Possible duplicate of [Convert string to title case with JavaScript](https://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript) – Daniel Jul 15 '17 at 17:52

1 Answers1

1

Convert string to title case with JavaScript is probably what you're looking for. It's called Title Case. Function pasted below for ease of use:

function toTitleCase(str)
{
    return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

credit to Greg Dean

JonLuca
  • 850
  • 6
  • 25