Java Script for first letter caps

Copy link to clipboard
Copied
Is there a java script that will ensure that my sentence is typed out with all of the first letter of each word is typed in a capital letter? I know how to make all of the words capital, but I am looking for a way to make just the first letter of each word a capital letter.
for example: How To Train Your Dragon
Thank you.
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi,
Please See the Below example for your refernce.
You can make changes accordingly.
function camelToDash(str) {
return str.replace(/\W+/g, '-')
.replace(/([a-z\d])([A-Z])/g, '$1-$2');
}
function dashToCamel(str) {
return str.replace(/\W+(.)/g, function (x, chr) {
return chr.toUpperCase();
})
}
var str1 = "devCurry";
str1 = camelToDash(str1);
var str2 = "dev-curry";
str2 = dashToCamel(str2);
alert(str1 + " " + str2);

Copy link to clipboard
Copied
I tried entering both of those (separately) into my form in the custom keystrok scipt, and they didn't seem to do anything? Am I doing this wrong?
Copy link to clipboard
Copied
Do you want to capitalize the first letter of every word or do you want to convert the words to Title Case where articles and other selected words are not capitalized?
For title case you could use the following document level function:
/*
* Title Caps
*
* Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
* Original by John Gruber - http://daringfireball.net/ - 10 May 2008
* License: http://www.opensource.org/licenses/mit-license.php
*/
function titleCaps(title){
var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
var parts = [], split = /[:.;?!] |(?: |^)["Ã’]/g, index = 0;
while (true) {
var m = split.exec(title);
parts.push( title.substring(index, m ? m.index : title.length)
.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
})
.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
return punct + upper(word);
})
.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
index = split.lastIndex;
if ( m ) parts.push( m[0] );
else break;
}
return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
.replace(/(['Õ])S\b/ig, "$1s")
.replace(/\b(AT&T|Q&A)\b/ig, function(all){
return all.toUpperCase();
});
};
function lower(word){
return word.toLowerCase();
}
function upper(word){
return word.substr(0,1).toUpperCase() + word.substr(1);
}
I would then put the following script in the custom format script, validation script, or custom calculation:
event.value = titleCaps(event.value);

Copy link to clipboard
Copied
I want to capitalize the first letter of every word in only some of the fields. Is there a script I can put into custom keystrok script? Everything I am getting here seems really complicated, and I am not sure what to do with it all.
Copy link to clipboard
Copied
A slight change tot the script:
function firstCaps(title){
/*
* adapted from Title Caps
*
* Ported to JavaScript By John Resig - http://ejohn.org/ - 21 May 2008
* Original by John Gruber - http://daringfireball.net/ - 10 May 2008
* License: http://www.opensource.org/licenses/mit-license.php
*/
// punctuation marks for spliting string into an array
var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
var parts = [], split = /[:.;?!] |(?: |^)["Ã’]/g, index = 0;
var m;
while (true) {
m = split.exec(title);
parts.push( title.substring(index, m ? m.index : title.length)
.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
})
.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
return punct + upper(word);
})
.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));
index = split.lastIndex;
if ( m ) parts.push( m[0] );
else break;
}
return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
.replace(/(['Õ])S\b/ig, "$1s")
.replace(/\b(AT&T|Q&A)\b/ig, function(all){
return all.toUpperCase();
});
}; // end firstCaps
function upper(word){
return word.substr(0,1).toUpperCase() + word.substr(1);
}
You now can call the firstCaps function to make all words start with a first letter capitalized. It is not easy to split all possible words from a string with punctuation and then recreate the modified string without losing the white space characters. It is very easy to make a string all upper case or all lower case.
You also have to know when to call the script. Custom keystroke scripts are tricky as one has to differentiate between pre-commitment and committed actions.
Copy link to clipboard
Copied
Here's a simpler approach that you can use as the field's custom Validate script:
// Convert all (g) word characters (\w) at word boundaries (\b) to uppercase
event.value = event.value.replace(/\b\w/g, function(char) {return char.toUpperCase();});
The conversion will take place when the value is committed, as opposed to as the user is entering the text.
Copy link to clipboard
Copied
I was wondering if i could change this script so that i can change the first letter in each line of text to Bold
so B195, B196, B197 would be B195, B196, B197
edit: sorry I just realized this was in PDF forum... i would want the script to work in illustrator.
Copy link to clipboard
Copied
Moved to the Illustrator forum
Acrobate du PDF, InDesigner et Photoshoptographe

