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

Java Script for first letter caps

Guest
Dec 10, 2012 Dec 10, 2012

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.

TOPICS
Scripting
2.4K
Translate
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
Adobe
Dec 10, 2012 Dec 10, 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);

Translate
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
Guest
Dec 11, 2012 Dec 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?

Translate
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
LEGEND ,
Dec 12, 2012 Dec 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);

Translate
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
Guest
Dec 12, 2012 Dec 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.

Translate
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
LEGEND ,
Dec 12, 2012 Dec 12, 2012

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.

Translate
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
LEGEND ,
Dec 12, 2012 Dec 12, 2012

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.

Translate
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
Participant ,
Mar 08, 2018 Mar 08, 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.

Translate
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 ,
Mar 08, 2018 Mar 08, 2018
LATEST

Moved to the Illustrator forum


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
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