Analytics


Google

Thursday, December 27, 2012

Java Script to Produce Proper Capitalization

The code is as follow:

/*
* 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(){
var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
this.titleCaps = function(title){
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);
}
})();

Code is taken from:
http://ejohn.org/blog/title-capitalization-in-javascript/


In order to get use the above, create a file and place the following code and save it. You can then run it locally to automatically show you the proper capitalization of the title. Just key in the title in the text box and click the submit button, it will then display the result above. Note that the script does not work in IE, it works in Chrome and Firefox.





=====================================

<head>
</head>
<script type="text/javascript">
/*
* 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(){
var small = "(a|an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
var punct = "([!\"#$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";

this.titleCaps = function(title){
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);
}
})();
</script>
<body>
<br />
<script>
function displayRslt()
{
var tmp;
var myTextFld;

try
{
  myTextFld = document.getElementById("tFld");

  if(myTextFld.value != "")
   {
     document.getElementById("tDisp").innerHTML=titleCaps(myTextFld.value);
   }
   else
     document.getElementById("tDisp").innerHTML="Nothing entered";
 }
catch(err)
  {
    document.getElementById("tDisp").innerHTML="error caught "+err.message;
   }
}
</script>
<p id="tDisp">Paragraph</p><br />
<form name="tForm" action="" method="GET">
Input values: <input type="text" id="tFld" value=""><br />
<Input type="button" name="submit" value="submit" onClick="displayRslt()">
</form>
</body>









======================================