Link to home
Start Free TrialLog in
Avatar of lserrano
lserrano

asked on

Sentence case

The following JS changes text input into Sentence case. But it only capitalizes the first word of the paragraph. Can you modify script so that the first letter of each sentnece is capitalized?

So, when I enter:

"yesterday i WENT To the STORe. it's an AWESOME store! very COOL."

Right now script fixes as:

"Yesterday i went to the store. it's an awesome store! very cool."

What I really need is:

"Yesterday I went to the store. It's an awesome store! Very cool."

Here's the script:

<script language="JavaScript">
<!-- Begin
function capsLc(){
if (navigator.appVersion.substring(0,1)=="2"){
navOld();
}
else navNew();
}
function navOld(){
txt=document.acc_form.description.value+" ";
txt=txt.toLowerCase();
txtl="";
while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
cmp=" "+wrd+" ";
if (tst.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);
document.acc_form.description.value=txtl;
}
function navNew(){
txt=document.acc_form.description.value+" ";
txt=txt.toLowerCase();
txtl="";
punc=",.?!:;)'";
punc+='"';
 while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
wrdpre="";
if (punc.indexOf(wrd.substring(0,1))>-1){
wrdpre=wrd.substring(0,1);
wrd=wrd.substring(1,wrd.length);
}
cmp=" "+wrd+" ";
for (var i=0;i<9;i++){
p=wrd.indexOf(punc.substring(i,i+1));
if (p==wrd.length-1){
cmp=" "+wrd.substring(0,wrd.length-1)+" ";
i=9;
   }
}
if (cmp.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrdpre+wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);
document.acc_form.description.value=txtl;
}
// End -->
</script>

<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10></TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc()">
</FORM>
</CENTER>
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

this solves the i to I problem

txtl=ltr+txtl.substring(1,txtl.length-1);
txtl=txtl.replace(/ i /g,' I ')
document.acc_form.description.value=txtl;
}
function toSentenceCase(str) {
   var res = "";
   var strEndSentence = ".!?";
   var foundEndSentence = false;
   
   for (i=0;i<str.length;i++) {
      if (i == 0) {
         res += str.charAt(i).toUpperCase();
      } else if (strEndSentence.indexOf(str.charAt(i)) >= 0) {
         res += str.charAt(i);
         foundEndSentence = true;
      } else if (foundEndSentence) {
            if (str.charAt(i) == " ") {
               res += " ";
            } else {
               res += str.charAt(i).toUpperCase();
               foundEndSentence = false;
            }
      } else if (str.charAt(i) == "i" && str.charAt(i-1) == " " && (str.charAt(i+1) == " " || i == str.length-1)) {
            res += str.charAt(i).toUpperCase();
      } else {
         res += str.charAt(i).toLowerCase();
         foundEndSentence = false;
      }      
   }
   
   return res;
}
alert (toSentenceCase("yesterday i WENT To the STORe. it's an AWESOME store! very COOL."));
and here is the final fix

<head>
<script language="JavaScript">

<!-- Begin
function capsLc(){
if (navigator.appVersion.substring(0,1)=="2"){
navOld();
}
else navNew();
}
function navOld(){
txt=document.acc_form.description.value+" ";
txt=txt.toLowerCase();
txtl="";
while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
cmp=" "+wrd+" ";
if (tst.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);
document.acc_form.description.value=txtl;
}
function navNew(){
txt=document.acc_form.description.value+" ";
txt=txt.toLowerCase();
txtl="";
punc=",.?!:;)'";
punc+='"';
while ((txt.length>0)&&(txt.indexOf(" ")>-1)){
pos=txt.indexOf(" ");
wrd=txt.substring(0,pos);
wrdpre="";
if (punc.indexOf(wrd.substring(0,1))>-1){
wrdpre=wrd.substring(0,1);
wrd=wrd.substring(1,wrd.length);
}
cmp=" "+wrd+" ";
for (var i=0;i<9;i++){
p=wrd.indexOf(punc.substring(i,i+1));
if (p==wrd.length-1){
cmp=" "+wrd.substring(0,wrd.length-1)+" ";
i=9;
}
}
if (cmp.indexOf(cmp)<0){
ltr=wrd.substring(0,1);
ltr=ltr.toUpperCase();
wrd=ltr+wrd.substring(1,wrd.length);
}
txtl+=wrdpre+wrd+" ";
txt=txt.substring((pos+1),txt.length);
}
ltr=txtl.substring(0,1);
ltr=ltr.toUpperCase();
txtl=ltr+txtl.substring(1,txtl.length-1);

txtl=txtl.replace(/ i /g,' I ')
while (/[\.\!\?\;\"\'][\ ]*[a-z]/.test(txtl))
{
l=txtl.match(/[\.\!\?\;\"\'][\ ]*[a-z]/)[0]
L=l.toUpperCase()
txtl=txtl.replace(l,L)
}

document.acc_form.description.value=txtl;
}
// End -->
</script>
</head>

<body>

<form NAME="acc_form">
  <div align="center"><center><p><textarea NAME="description" ROWS="10"></textarea> <input
  TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc()"> </p>
  </center></div>
</form>
</body>
</html>
why does it need to be so long? ;)
my entry in the least amout of code category is

<head>
<script language="JavaScript">
function capsLc(){
txtl=document.acc_form.description.value
txtl=txtl.toLowerCase()
txtl=txtl.replace(/ i /g,' I ')
while (/[\.\!\?\;\"][\ ]*[a-z]/.test(txtl)){
 l=txtl.match(/[\.\!\?\;\"][\ ]*[a-z]/)[0]
 L=l.toUpperCase()
 txtl=txtl.replace(l,L)
}
document.acc_form.description.value=txtl;
}
</script>
</head>

<body>
<form NAME="acc_form">
  <div align="center"><center><p><textarea NAME="description" ROWS="10"></textarea> <input
  TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc()"> </p>
  </center></div>
</form>
</body>
</html>
forgot newlines if they are no indented, shld read

<script language="JavaScript">
function capsLc(){
txtl='document.acc_form.description.value
txtl=txtl.toLowerCase()
txtl=txtl.replace(/ i /g,' I ')
while (/[\n\r\.\!\?\;\"][\ ]*[a-z]/.test(txtl)){
 l=txtl.match(/[\n\r\.\!\?\;\"][\ ]*[a-z]/)[0]
 L=l.toUpperCase()
 txtl=txtl.replace(l,L)
}
document.acc_form.description.value=txtl;
}
</script>

shld be txtl=document.acc_form.description.value ie remove '
Avatar of Zvonko
Here my version:


<script language="JavaScript">
<!-- Begin
var letter = "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var newSent = ".!?";
function capsLc(theButton){
  desc = theButton.form.description;
  descText = " "+desc.value;
  descText = Capitalize(descText, 0);
  for(var n=2;n<descText.length;n++){
    if(newSent.indexOf(descText.substr(n,1))>-1){
      descText = Capitalize(descText, n);
    }
  }
  desc.value = descText.substr(1);
}
function Capitalize(theText, nextPos){
  for(var i=nextPos;i<theText.length;i++){
    if(letter.indexOf(theText.substr(i,1))>-1){
      theText = theText.substr(0,i)+
         theText.substr(i,1).toUpperCase()+
         theText.substr(i+1).toLowerCase();
      break;      
    }
  }
  return theText;
}
// End -->
</script>

<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10>yesterday i WENT To the STORe. it's an AWESOME store! very COOL.</TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc(this)">
</FORM>
</CENTER>





Small Note: my version does work WITHOUT   RegExp!

Smaller note: mine does too
try this

function navNew(){
txt=document.acc_form.description.value+" ";
txt=txt.toLowerCase();
txt1="";
punc=",.?!:;)";
punc+='"';
puncFound = true;
for (i=0; i < txt.length; i++) {
  curChar = txt.substring(i, i+1);
  if (puncFound && (curChar != " ") && (punc.indexOf(curChar) == -1)) {
    puncFound = false;
      curChar = curChar.toUpperCase();
  } else if (punc.indexOf(curChar) > 0) {
     puncFound = true;
  } else if (curChar == 'i') {
     if (i == 0 || ((punc + " ").indexOf(txt.substring(i - 1, i)) > 0 && (punc + " ").indexOf(txt.substring(i+1, i+2)) > 0)) {
         curChar = curChar.toUpperCase();
       }
  }
  txt1 = txt1 + curChar;
}
document.acc_form.description.value=txt1;
}
@bobbit31: check yours with initial blanks.

@lserrano: My failed for uppercase I
Here is the "I" correction working also when not separated on both sides by blank.

<script language="JavaScript">
<!-- Begin
var letter = "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var newSent = ".!?";
function capsLc(theButton){
  desc = theButton.form.description;
  descText = " "+desc.value;
  descText = Capitalize(descText, 0);
  for(var n=2;n<descText.length;n++){
    if(newSent.indexOf(descText.substr(n,1))>-1){
      descText = Capitalize(descText, n);
    }
  }
  descText = iAm(descText);
  desc.value = descText.substr(1);
}
function Capitalize(theText, nextPos){
  for(var i=nextPos;i<theText.length;i++){
    if(letter.indexOf(theText.substr(i,1))>-1){
      theText = theText.substr(0,i)+
         theText.substr(i,1).toUpperCase()+
         theText.substr(i+1).toLowerCase();
      break;      
    }
  }
  return theText;
}
function iAm(theText){
  for(var i=0;i<theText.length;i++){
    if(letter.indexOf(theText.substr(i,1))<0 &&
       theText.substr(i+1,1)=="i" &&
       letter.indexOf(theText.substr(i+2,1))<0){
      theText = theText.substr(0,i+1)+"I"+theText.substr(i+2);
    }
  }
  return theText;
}
// End -->
</script>

<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10> aaa  yesterday i WENT To the STORe. it's an AWESOME store! very COOL.</TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc(this)">
</FORM>
</CENTER>

here is my version:
==================
<div id=myText>"yesterday i WENT To the STORe. it's an AWESOME store! very COOL."</div>
"Yesterday I went to the store. It's an awesome store! Very cool."<br>
<script>
function runit()
{
      var objMyText=document.getElementById("myText");
      objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/^.(.){1}|i|\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<button onclick=runit()>runit</button>
of course you don't need a quotation marks ;)
==========
<div id=myText>yesterday i WENT To the STORe. it's an AWESOME store! very COOL.</div>
Yesterday I went to the store. It's an awesome store! Very cool.<br>
<script>
function runit()
{
    var objMyText=document.getElementById("myText");
    objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/^(.){1}|i|\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<button onclick=runit()>runit</button>
re: "Small Note: my version does work WITHOUT   RegExp!"

so what!
So what?

You need no version checking like this:

    if (navigator.appVersion.substring(0,1)=="2"){
       navOld();
      ....



re:  (navigator.appVersion.substring(0,1)=="2")

is that a joke?  So what else do you remove from the JS tool box if you check back to "2", :)
Avatar of lserrano
lserrano

ASKER

@zvonko: Is it possible to also check for multiple spaces between words and reduce to just 1 space? So if user enters "Yesterday   I went to   the store." Script would change to "Yesterday I went to the store."
Yes, that is easy.
But please state how important is for you to avoid RegExp.
Because RegExp make life much more easier.

yeah, RegExp is power!

<textarea id=myText style=width:600px;height:200px>
yesterday  i         WENT To the   STORe. it's    an AWESOME store!         very COOL.
</textarea>

<script>
function runit()
{
    var objMyText=document.getElementById("myText");
    objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/  /g," ").replace(/^(.){1}|i|\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<br>
<button onclick=runit()>runit</button>
Here the NON regexp solution :)

<script language="JavaScript">
<!-- Begin
var letter = "abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var newSent = ".!?";
function capsLc(theButton){
  desc = theButton.form.description;
  descText = " "+desc.value;
  descText = Capitalize(descText, 0);
  for(var n=2;n<descText.length;n++){
    if(newSent.indexOf(descText.substr(n,1))>-1){
      descText = Capitalize(descText, n);
    }
  }
  descText = iAm(fullTrim(descText));
  desc.value = descText.substr(1);
}
function Capitalize(theText, nextPos){
  for(var i=nextPos;i<theText.length;i++){
    if(letter.indexOf(theText.substr(i,1))>-1){
      theText = theText.substr(0,i)+
         theText.substr(i,1).toUpperCase()+
         theText.substr(i+1).toLowerCase();
      break;      
    }
  }
  return theText;
}
function iAm(theText){
  for(var i=0;i<theText.length;i++){
    if(letter.indexOf(theText.substr(i,1))<0 &&
       theText.substr(i+1,1)=="i" &&
       letter.indexOf(theText.substr(i+2,1))<0){
      theText = theText.substr(0,i+1)+"I"+theText.substr(i+2);
    }
  }
  return theText;
}
function fullTrim(theText){
  for(var i=0;i<theText.length;i++){
    while(theText.substr(i,2)=="  "){
      theText = theText.substr(0,i)+theText.substr(i+1);
    }
  }
  return theText;
}
// End -->
</script>

<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10> yesterday i WENT To the STORe. it's   an   AWESOME   store! very COOL.</TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc(this)">
</FORM>
</CENTER>

@devic: you have also the leading blank problem.
sorry :)
===============

<textarea id=myText style=width:600px;height:200px>
yesterday                  i         WENT To                              the   STORe. it's    an AWESOME store!                        very COOL.
</textarea>

<script>
function runit()
{
   var objMyText=document.getElementById("myText");
   objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/[ ]{1,}/g," ").replace(/^(.){1}|i|\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<br>
<button onclick=runit()>runit</button>
Still not working.
Put a blank before yesterday
First single blank should not disappear and next Character should be Capitalized.
ach so, i  did not test with blank at the beginn of string.
thanks Zvonko ;)

==================
<textarea id=myText style=width:600px;height:200px>
        yesterday                  i         WENT To                              the   STORe. it's    an AWESOME store!                        very COOL.
</textarea>

<script>
function runit()
{
   var objMyText=document.getElementById("myText");
   objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/[ ]{1,}/g," ").replace(/^ ?(.){1}|i|\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<br>
<button onclick=runit()>runit</button>
Nearly perfect: you remove single leading blank.
But I think that is normal in any written English text.
By the way: your replace() RegExp lambda function call limits your method to JavaScript1.3
yes Zvonko, we spoke already about this, but i like lambda expression ;)
BTW JavaScript Version Statistic:

 1.3 98.87%
 1.0 0.57%
 1.2 0.55%
 other 0.02%

so for 98.87% is ok ;)

this implements a dictionary of proper names and 2 spaces beetween sentences

eg input

" hi jOhn     and mary! hOw are    i am cINdy.
 I    am from    THe USA    ARE   yoU from    THe   UK?"

ouput

" Hi John and Mary!  How are I am Cindy.
 I am from the USA are you from the UK?"


<script language="JavaScript">
capString='Dave,John,Mary,Randy,Cindy,USA,UK,Canada,France'
capArray=capString.split(',')

function capsLc(txt){
txt=txt.toLowerCase()
txt=txt.replace(/ i([^a-z])/gi,' I'+'$1')
while (/[\n\r\.\!\?\;\"][\ ]*[a-z]/.test(txt)){
 l=txt.match(/[\n\r\.\!\?\;\"][\ ]*[a-z]/)[0]
 L=l.toUpperCase()
 txt=txt.replace(l,L)
}
txt=txt.replace(/((\w) +(\w))/g,'$2'+' '+'$3')
txt.replace
words=txt.match(/[a-z]+/gi)
for (i=0;i<words.length;i++)
 for (j=0;j<capArray.length;j++){
   if (words[i].toLowerCase()==capArray[j].toLowerCase())      
    txt=txt.replace(words[i],capArray[j])
 }

return txt
}

txt='" hi jOhn     and mary! hOw are    i am cINdy.'+'\n'+' I    am from    THe USA    ARE   yoU from    THe   UK?"'
alert(txt)
alert(capsLc(txt))
</script>
this argument from Zvonko is a nonesense (ie Ver 1.3 =98.87%)
Oh so you do not recommend using regex? see http:Q_20893061.html
perhaps you should give the points back :)
@zvonko: One problem I found with your solution is when I type the word "opening" it changes to "openIng."
<textarea id=myText style=width:600px;height:200px>
        yesterday          i         WENT To                              the  opening  STORe. it's    an AWESOME store!                        very COOL.
</textarea>

<script>
function runit()
{
   var objMyText=document.getElementById("myText");
   objMyText.innerHTML=objMyText.innerHTML.toLowerCase().replace(/[ ]{1,}/g," ").replace(/^ ?(.){1}| (i) |\. (.)|\! (.)/g, function(p1,p2){return p1.toUpperCase()});
}
</script>
<br>
<button onclick=runit()>runit</button>
Last version, it  has a proper name dictionary and a spell check dictionary. The code needs some tuneing but the basic idea is there (must be bored tonight)

"tEH comPtr     taht i got FroM cINDY    in thE UsA, iS bRoKE!
wHy? oH DEar, Oh dEAR"

becomes

"The computer that I got from Cindy in the USA, is broke!
Why? Oh dear, oh dear"


<head>
<script language="JavaScript">
capString='Dave,John,Mary,Randy,Cindy,USA,UK,Canada,France'
capArray=capString.split(',')
spellString="the,teh,hte;that,taht;computer,comptr,computr"
spellArray=spellString.split(';')

function capsLc(txt){
txt=txt.toLowerCase()
txt=txt.replace(/((\w[^ ]) {2,}(\w))/g,'$2'+' '+'$3')
txt=txt.replace(/ i([^a-z])/gi,' I'+'$1')

words=txt.match(/[a-z]+/gi)
for (i=0;i<words.length;i++){
  for (j=0;j<capArray.length;j++)
  if (words[i].toLowerCase()==capArray[j].toLowerCase())
       txt=txt.replace(words[i],capArray[j])
  for (j=0;j<spellArray.length;j++){
   reg=new RegExp(words[i].toLowerCase(),'gi')
   if (  reg.test(spellArray[j]) )
   txt=txt.replace(words[i],spellArray[j].split(',')[0])        
   }            
 }

while (/[\n\r\.\!\?\;\"][\ ]*[a-z]/.test(txt)){
  l=txt.match(/[\n\r\.\!\?\;\"][\ ]*[a-z]/)[0]
  L=l.toUpperCase()
  txt=txt.replace(l,L)
}

return txt
}
</script>
</head>

<body>

<p><textarea name="t1" rows="20" cols="40"></textarea></p>

<p>sample text:<br>
&quot;tEH comPtr taht i got FroM cINDY in thE UsA, iS bRoKE!<br>
wHy? oH DEar, Oh dEAR&quot; </p>

<p><input type="button" value="edit" onclick=" t1.value=capsLc(t1.value)"></p>
</body>
</html>
Sorry, my alphabet was not complete.
Take this:

var letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

var letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
@GwynforWeb: Your solution works quite well, except from "tEH comPtr taht i got FroM cINDY in thE UsA, iS bRoKE! wHy? oH DEar, Oh dEAR" I get "the computer that I got from Cindy in the USA, is broke! Why? Oh dear, oh dear". Notice the first word is "the" but should be "The"
BTW, can you also ensure that there is 1 space after the end of each sentence and bfore the beginning of the next sentence?

i.e.,
"Good stuff.  Great!" should be changed to "Good stuff. Great!"

And, some words like eBooks and eCourse are okay, so can I have a list of these which are acceptable and should NOT be changed to lowercase?
BTW, points to be increased to 800 as soon as I can figure out how to increase them. ;-)
lserrano,
     Heck, the 'the' problem only happens sometimes, i missed the subcase. Try this, (there is only 1 space between sentences if you check withe the cursor)

<head>
<script language="JavaScript">
capString='Dave,John,Mary,Randy,Cindy,USA,UK,Canada,France,eBook,eCourse'
capArray=capString.split(',')
spellString="the,teh,hte;that,taht;computer,comptr,computr"
spellArray=spellString.split(';')

function capsLc(txt){
txt='"'+txt.toLowerCase()

txt=txt.replace(/([\.\!\?\;\"\:\,])[\ ]*([a-z])/g,'$1'+' '+'$2')
txt=txt.replace(/ i([^a-z])/gi,' I'+'$1')

words=txt.match(/[a-z]+/gi)
for (i=0;i<words.length;i++){
 for (j=0;j<capArray.length;j++)
  if (words[i].toLowerCase()==capArray[j].toLowerCase())
   txt=txt.replace(words[i],capArray[j])

for (j=0;j<spellArray.length;j++){
 reg=new RegExp(words[i].toLowerCase(),'gi')
 if ( reg.test(spellArray[j]) )
  txt=txt.replace(words[i],spellArray[j].split(',')[0])
}
}

while (/[\n\r\.\!\?\;\"\:\,][\ ]*[a-z]/.test(txt)){
 l=txt.match(/[\n\r\.\!\?\;\"\:\,][\ ]*[a-z]/)[0]
 L=l.toUpperCase()
 txt=txt.replace(l,L)
}

return txt.replace(/^" /,'')
}
</script>

<title></title>
</head>

<body>

<p><textarea name="t1" rows="20" cols="40"></textarea></p>

<p>sample text:<br>
&quot;tEH comPtr taht i got FroM cINDY in thE UsA iS bRoKE! I am DOIng an ECOurse from the
EBook. <br>
wHy? oH DEar, Oh dEAR.<br> Good stuff.Great!&quot;</p>

<p><input type="button" value="edit" onclick=" t1.value=capsLc(t1.value)"></p>
</body>
</html>



Just one strange thing...

When I include ANY word in spellString which contains the letter "i" the script does funny translations.
Try using my spellString:

spellString="the,teh,hte;that,taht;recieve,receive;website,web site;entrepeneur,entrepreneur;continous,continuous;homebased,home-based;home based,home-based;managerment,management;porspects,prospects;strategise,strategize"

Also, can you allow for words like "home-based" and "web site" in spellString please.
Iserrano,

Did you try my code...
Can someone address my last comments? I'll then accept your answer as my preferred solution.

Here my RegExp version:

<head>
<title>Capitalize</title>
<script>
capArray= 'I,Dave,John,Mary,Randy,Cindy,USA,UK,Canada,France,eBook,eCourse'.split(',');
spellArray=("the,teh,hte;that,taht;recieve,receive;website,web site;"+
           "entrepeneur,entrepreneur;continous,continuous;"+
           "homebased,home-based;home based,home-based;managerment,management;"+
           "prospects,porspects;strategise,strategize;computer,comptr,computr").split(';');
function capsLc(theButton){
  desc = theButton.form.description;
  descText = "."+desc.value.toLowerCase();
  descText = descText.replace(/\ +/g," ");
  for(i=0;i<spellArray.length;i++){
    spellWord = spellArray[i].split(',');
    for(j=1;j<spellWord.length;j++){
      descText = descText.replace(new RegExp("\\b"+spellWord[j]+"\\b", "gi"), spellWord[0]);
    }
  }
  descText = descText.replace(/[\.\!\?]\s*([a-z])/g, function (p1,p2){return p1.toUpperCase()});
  for(i=0;i<capArray.length;i++){
    descText = descText.replace(new RegExp("\\b"+capArray[i]+"\\b", "gi"), capArray[i]);
  }
  desc.value = descText.substr(1);
}
</script>
</head>
<body>
<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10>tEH comPtr taht i got FroM cINDY in thE UsA, iS bRoKE! wHy? oH DEar, Oh dEAR!</TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc(this)">
</FORM>
</CENTER>
</body>
</html>

I have addresdsed your last request.
Now are all spell correction requests done first and then is the word array applied.
Now is "I" aslo a word of that capsArray.

Strange, but although you have recieve,receive; in your spellArray, the mis-spelled word doesn't get changed. Also, the word "website" is not changed to "web site" and "home based" is not changed to "home-based"

Thanks for all your help.

BTW, how do I increase these points to 800?
By offering a second question with three hundred points and reference to this question.

And the syntax of the spellArray list is this:

spellArray = "toWord0, fromWord1, fromWord2; ...";

That does say: if in text are the words fromWord1 or fromWord2 found, then are all occurances of those words changed to toWord0.

Thanks Zvonko. What I was aksing is how do I increase the points from 500 to 800? Right now when I award these points to you, you'll only get 500, but I want to give you 800. I can't see where to change the points.

Also, one last thing, when a user enters quotation marks or exclamation points, can you remove quotation marks and replace exclamation points with dots?

i.e.,
Entered:
This product will "blow your socks off" or it's completely free!

Change to:
This product will blow your socks off or it's completely free.

BTW, does this script work with MSIE and Netscape?
Here the version with your last requirements.

<head>
<title>Capitalize</title>
<script>
capArray= 'I,Dave,John,Mary,Randy,Cindy,USA,UK,Canada,France,eBook,eCourse'.split(',');
spellArray=("the,teh,hte;that,taht;recieve,receive;website,web site;"+
           "entrepeneur,entrepreneur;continous,continuous;"+
           "homebased,home-based;home based,home-based;managerment,management;"+
           "prospects,porspects;strategise,strategize;computer,comptr,computr").split(';');
function capsLc(theButton){
  desc = theButton.form.description;
  descText = "."+desc.value.toLowerCase().replace(/\"/g,"").replace(/\!/g,'.');
  descText = descText.replace(/\ +/g," ");
  for(i=0;i<spellArray.length;i++){
    spellWord = spellArray[i].split(',');
    for(j=1;j<spellWord.length;j++){
      descText = descText.replace(new RegExp("\\b"+spellWord[j]+"\\b", "gi"), spellWord[0]);
    }
  }
  descText = descText.replace(/[\.\!\?]\s*([a-z])/g, function (p1,p2){return p1.toUpperCase()});
  for(i=0;i<capArray.length;i++){
    descText = descText.replace(new RegExp("\\b"+capArray[i]+"\\b", "gi"), capArray[i]);
  }
  desc.value = descText.substr(1);
}
</script>
</head>
<body>
<FORM NAME="acc_form">
<CENTER>
<textarea NAME="description" ROWS=10>tEH comPtr taht i got FroM cINDY in thE UsA, iS bRoKE! wHy? oH DEar, Oh dEAR!
This product will "blow your socks off" or it's completely free!
</TEXTAREA>
<INPUT TYPE="button" NAME="html1" VALUE=" Submit " onClick="capsLc(this)">
</FORM>
</CENTER>
</body>
</html>


And I need no eight hundred points. This five are enough.

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Zvonko. ;-)
You are welcome.