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?
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();
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();
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.)
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!
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
Copy link to clipboard
Copied
This is nice and intuitive!
Copy link to clipboard
Copied
Yes. There are 4 possibilities:
CaseChangeType.LOWERCASE
CaseChangeType.SENTENCECASE
CaseChangeType.TITLECASE
CaseChangeType.UPPERCASE
Copy link to clipboard
Copied
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.