例如:
HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?或者:how are you? i am fine, thank you. where are you from? i am from china. what about you?转换成正常句子的大小写,就是句子的第一个单词,首字母是大写的,句子的其余部分是小写的:

How are you? I am fine, thank you. Where are you from? I am from China. What about you?我历遍整个互联网,只找到下面的JavaScript例子。第一个例子,是把整篇文章的第一个字母变成大写,其余的是小写。(现实中没什么用)。第二个例子,是把所有的单词的首字母都变成大写,类似英文标题。但它们都不是我想要的。谁能帮我修改一下,完成我要的功能?

 function wordUpperCase(word){
  var wLength = word.length;  
  if(wLength > 0){
   var ss = (word.substring(0,1).toUpperCase()) + (word.substring(1,wLength).toLowerCase()+" ");
  }
  return ss;
 }function titleUpperCase(title){
  var upperTitle = '';
  var array = title.split(' ');
  for(i = 0 ;i < array.length ; i++){
   upperTitle += wordUpperCase(array[i]);     
  }
  return upperTitle; 
 } 

解决方案 »

  1.   

    var str="HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?";
    var toSen=function(str){
        return str.toLowerCase().replace(/((?:[.?!]|^) ?[a-z])/g,function($,$1){
            return $1.toUpperCase()
        })
    };
    var nstr=toSen(str);
    alert(nstr);
      

  2.   


    <script type="text/javascript">
    var str = "HOW ARE YOU? I AM FINE, THANK YOU. WHERE ARE YOU FROM? I AM FROM CHINA. WHAT ABOUT YOU?";
    function toUpper(str) {
    return str.toLowerCase().replace(/(^|\?|\.)(\s*)([a-z])/g, function($0, $1, $2, $3) {
    return $1 + $2 + $3.toUpperCase();
    });
    }
    alert(toUpper(str));
    </script>
      

  3.   

    段落首字母和句子首字母这个容易实现,但是如果要加上一点其他规则(如 I 或其他特殊词语要大写,或者引号内句子首字母大写什么的)的话就有点麻烦,那个只能在之后加上另外的正则来替换。function setUp() {
    var str = 'i am happy to join with you today in what will go down in history as the greatest demonstration for freedom in the history of our nation.\r\nfive score years ago, a great american, in whose symbolic shadow we stand today, signed the emancipation proclamation. this momentous decree came as a great beacon light of hope to millions of negro slaves who had been seared in the flames of withering injustice. it came as a joyous daybreak to end the long night of their captivity.\r\nbut one hundred years later, the negro still is not free. one hundred years later, the life of the negro is still sadly crippled by the manacles of segregation and the chains of discrimination. one hundred years later, the negro lives on a lonely island of poverty in the midst of a vast ocean of material prosperity. one hundred years later, the negro is still languished in the corners of american society and finds himself an exile in his own land. and so we\'ve come here today to dramatize a shameful condition.';
    //在网页中通过innerText得到的数据里换行符为 \r\n,这是 windows默认的换行符。
    //alert(str.toLowerCase())
    setp_Up(str);
    }
    function setp_Up(temp){
    temp = temp.toLowerCase();
    temp = temp.replace(/(^|\n|[\.?!]\s?)([a-zA-Z])/g, function($0, $1, $2) { return $1 + $2.toUpperCase();});
    alert(temp);
    }
    setUp();
      

  4.   

    可以用JS + CSS 更简单下面的CSS可以让文本内的所有单词的首字母大写。
    .firstLetterUpCase{
        text-transform:lowercase;
    }不过之前要将文本内的所以字母都改为小写txt.toLowerCase();不过有一个小缺憾:如果是这样: aaa你好a会成这样:Aaa你好A;就是说不认中文,如果有,就会被认为是空格一样。
      

  5.   

    晕,错了是这样:
       text-transform:capitalize;