Link to home
Start Free TrialLog in
Avatar of calzone
calzone

asked on

Convert All Upper case to mixed case......

Does anybody know how to convert a string that is all in uppercase to a mixed case string in javascript. I know how to do this in PHP but I need to do this with javascript. I can't seem to find anything to do this.

Thanks
Cal
Avatar of j3one
j3one

Microsoft word will change it all to lowercase, then you can go back through and hit the uppercase parts...
I do not understand this question, could you give an example.
Avatar of calzone

ASKER

I want to do this with a javascript function or command on a web page. I have a variable on a web page that is all uppercase. And I want to convert it to have The first letter of a word uppercase and the remaining letters in the word lowercase.

CONVERT THIS TEXT

into

Convert This Text
<script language="Javascript">
function firstLetter_Upper(theWord){
   var words = theWord.split(' ');
   var newWord='';
    for(i=0; i < words.length; i++)
        newWord += words[i].charAt(0).toUpperCase() + words[i].substring(1,words[i].length) + ' ';

alert(newWord);
return newWord;

}
</script>
this is shorter

<script language="Javascript">
function firstLetter_Upper(theWord){
 newWord=theWord.replace(/^(.)/,function (d){return d.toUpperCase()})
 return newWord;
}

alert(firstLetter_Upper('abc'))
</script>
<script language="Javascript">
function firstLetter_Upper(theWord){
 theWord=' '+theWord
 newWord=theWord.replace(/ (.)/g,function (d){return d.toUpperCase()})
 return newWord.replace(/ /,'');
}

alert(firstLetter_Upper('abc def ghi'))
</script>
Avatar of calzone

ASKER

Both of these functions are working if you start from a word that is all lowercase. But if you start from a word that is all uppercase they do not work. I am starting with all uppercase and need to get first letter upper and remaining letters lower.

Thanks
Cal
Avatar of devic
if i undestood good, this wants calzone
=============================
<script>
function firstLetter_Upper(str)
{
       var strArr=str.split(" ");
       for(var i=0;i<strArr.length;i++)
       strArr[i]=strArr[i].toLowerCase().replace(/^([ a-z])/g,function (d){return d.toUpperCase()})
       return strArr.join(" ");
}

alert(firstLetter_Upper('CONVERT THIS TEXT '))
</script>
ASKER CERTIFIED SOLUTION
Avatar of Bustarooms
Bustarooms
Flag of United States of America 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
Avatar of calzone

ASKER

Works great.

Thanks for the help Bustarooms.

Cal
and my? doesn't work?