Convert a Sentence to InitCap / camel Case / Proper Case

15,522

Solution 1

You can try:

  • Converting the entire string to lowercase
  • Then use replace() method to convert the first letter to convert first letter of each word to upper case

str = "hEllo woRld";
String.prototype.initCap = function () {
   return this.toLowerCase().replace(/(?:^|\s)[a-z]/g, function (m) {
      return m.toUpperCase();
   });
};
console.log(str.initCap());

Solution 2

If you want to account for names with an apostrophe/dash or if a space could potentially be omitted after a period between sentences, then you might want to use \b (beg or end of word) instead of \s (whitespace) in your regular expression to capitalize any letter after a space, apostrophe, period, dash, etc.

str = "hEllo billie-ray o'mALLEY-o'rouke.Please come on in.";
String.prototype.initCap = function () {
   return this.toLowerCase().replace(/(?:^|\b)[a-z]/g, function (m) {
      return m.toUpperCase();
   });
};
alert(str.initCap());

OUTPUT: Hello Billie-Ray O'Malley-O'Rouke.Please Come On In.

Solution 3

str="hello";
init_cap=str[0].toUpperCase() + str.substring(1,str.length).toLowerCase();

alert(init_cap);

where str[0] gives 'h' and toUpperCase() function will convert it to 'H' and rest of the characters in the string are converted to lowercase by toLowerCase() function.

Share:
15,522

Related videos on Youtube

Tushar Gupta - curioustushar
Author by

Tushar Gupta - curioustushar

Tech enthusiast, Open source evangelist, Curious learner. Curiosity replaces I have to with I want to. #SOreadyToHelp

Updated on September 15, 2022

Comments

  • Tushar Gupta - curioustushar
    Tushar Gupta - curioustushar over 1 year

    I have made this code. I want a small regexp for this.

    String.prototype.capitalize = function() {
        return this.charAt(0).toUpperCase() + this.slice(1);
    } 
    String.prototype.initCap = function () {
        var new_str = this.split(' '),
            i,
            arr = [];
        for (i = 0; i < new_str.length; i++) {
            arr.push(initCap(new_str[i]).capitalize());
        }
        return arr.join(' ');
    }
    alert("hello world".initCap());
    

    Fiddle

    What i want

    "hello world".initCap() => Hello World

    "hEllo woRld".initCap() => Hello World

    my above code gives me solution but i want a better and faster solution with regex

  • Tushar Gupta - curioustushar
    Tushar Gupta - curioustushar over 10 years
    Brother you are very fast on keys and accurate too .I tried your code DEMO
  • Stephen P
    Stephen P over 5 years
    ...but accept that you'll never be 100% correct. All bets are off when you get into people's names. I know someone whose last-name (family-name) when written properly, is d'Ellerba but it almost always comes out as D'Ellerba or D'ellerba or Dellerba -- when searching I also found someone named Dell'Erba
  • Arvind Kumar Avinash
    Arvind Kumar Avinash about 3 years
    It seems you misunderstood the requirement e.g. your solution will output Hello world how are you for hEllo woRld how aRe you whereas it is supposed to output Hello World How Are You.