Skip to main content
subieguy2
Inspiring
November 10, 2017
Answered

Capitalize first letter of each word with regexp

  • November 10, 2017
  • 3 replies
  • 3039 views

I am trying to capitalize the first letter of each word from a user input. The user types an input into a gui for example:

the name and the title

I want my regexp to make it look like this....

The Name and The Title

So capitalize first letter of any word except and.

I thought what I had written was correct but it doesn't seem to be working. I wrote 2 different options and neither work.

Any help would be appreciated!

var formattedOutput = formatDesc(userInput);                   

formattedOutput.replace(/\b[a-z]/g, function(f){return f.toUpperCase();})

That is not working or addressing the word and either.

I also tried it as a function like this

function titleCase(str) {

var splitStr = str.toLowerCase().split(' ');

for (var i = 0; i < splitStr.length; i++) {

   if (splitStr.length < splitStr.length) {

     splitStr.charAt(0).toUpperCase();    

   }

      str = splitStr.join(' ');

}

return str;

}

var formattedOutput = formatDesc(userInput);

titleCase(formattedOutput);

Still no dice. What am I doing wrong here?

This topic has been closed for replies.
Correct answer Silly-V

This worked for me:

#target illustrator

function test(){

  var str = "one and two";

  var strArr = str.split(" ");

  for(var i=0; i<strArr.length; i++){

    if(strArr == "and"){

      continue;

    } else {

      strArr = strArr.charAt(0).toUpperCase() + strArr.substr(1,);

    }

  };

  alert(strArr.join(" "));

};

test();

3 replies

subieguy2
subieguy2Author
Inspiring
November 14, 2017

Thank you both for your help and input! I marked Silly-V​ as a correct answer for responding first and it does work for me! However I think I will use the input you gave @Jongware and stick with the regexp...which also worked out great! Love to have more than one option for future applications and building my mediocre programming skills  

Thanks again to you both!

pixxxelschubser
Community Expert
Community Expert
November 16, 2017

Only another possibility (without loop or regex):

var aTF = app.activeDocument.textFrames.add();

aTF.contents = "one and two and three.";

alert ("");

aTF.textRange.changeCaseTo (CaseChangeType.TITLECASE);

redraw();

Have fun

Silly-V
Legend
November 20, 2017

This is nice and intuitive!

Jongware
Community Expert
Community Expert
November 13, 2017

Your first attempt (which is exactly what I would have tried first, so a +1 from me!) works. You forgot only one thing: 'replace' returns a modified string, it does not change the original string. Put 'formattedOutput =' before that line to make it work.

(And your second attempt suffers the same problem.)

Silly-V
Silly-VCorrect answer
Legend
November 10, 2017

This worked for me:

#target illustrator

function test(){

  var str = "one and two";

  var strArr = str.split(" ");

  for(var i=0; i<strArr.length; i++){

    if(strArr == "and"){

      continue;

    } else {

      strArr = strArr.charAt(0).toUpperCase() + strArr.substr(1,);

    }

  };

  alert(strArr.join(" "));

};

test();