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?

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.