• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Capitalize first letter of each word with regexp

Engaged ,
Nov 10, 2017 Nov 10, 2017

Copy link to clipboard

Copied

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?

TOPICS
Scripting

Views

2.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Nov 10, 2017 Nov 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();

Votes

Translate

Translate
Adobe
Valorous Hero ,
Nov 10, 2017 Nov 10, 2017

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

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.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2017 Nov 16, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

This is nice and intuitive!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

Yes. There are 4 possibilities:

CaseChangeType.LOWERCASE

CaseChangeType.SENTENCECASE

CaseChangeType.TITLECASE

CaseChangeType.UPPERCASE

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 21, 2017 Nov 21, 2017

Copy link to clipboard

Copied

LATEST

Well, maybe not. OP wrote

> ... the first letter of each word from a user input. The user types an input into a gui ...

which I understand it to be a ScriptUI input field. For this method to work, the text would first need to be inserted into an Illy document.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines