Skip to main content
December 10, 2012
Question

Java Script for first letter caps

  • December 10, 2012
  • 2 replies
  • 2557 views

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.

This topic has been closed for replies.

2 replies

Inspiring
December 12, 2012

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

December 12, 2012

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.

JR Boulay
Community Expert
Community Expert
March 8, 2018

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.


Moved to the Illustrator forum

Acrobate du PDF, InDesigner et Photoshopographe
Mandeep5062
Participating Frequently
December 11, 2012

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

December 11, 2012

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?