用JavaScriptRegex實現將字串中小寫字母轉換成大寫字母

來源:互聯網
上載者:User

**
 * 功能說明:將字串中的所有小寫字母轉換成大寫字母
 * 輸入參數:待轉換字串
 * 輸出:大寫字母字串
 * 使用舉例:function test_2(){
 *           var str="abcdefdsfasdfdsaf";
 *           var result=toUpperCase(str);
 *           alert(result);
 *         }
 */

function toUpperCase(str){
   var regExp=/[a-z]/g;
   return str.replace(regExp,function(sMatch){
  switch(sMatch){
   case 'a':{
    return "A";
    break;
   }
   case 'b':{
    return "B";
    break;
   }
   case 'c':{
    return "C";
    break;
   }
   case 'd':{
    return "D";
    break;
   }
   case 'e':{
    return "E";
    break;
   }
   case 'f':{
    return "F";
    break;
   }
   case 'g':{
    return "G";
    break;
   }
   case 'h':{
    return "H";
    break;
   }
   case 'i':{
    return "I";
    break;
   }
   case 'j':{
    return "J";
    break;
   }
   case 'k':{
    return "K";
    break;
   }
   case 'l':{
    return "L";
    break;
   }
   case 'm':{
    return "M";
    break;
   }
   case 'n':{
    return "N";
    break;
   }
   case 'o':{
    return "O";
    break;
   }
   case 'p':{
    return "P";
    break;
   }
   case 'q':{
    return "Q";
    break;
   }
   case 'r':{
    return "R";
    break;
   }
   case 's':{
    return "S";
    break;
   }
   case 't':{
    return "T";
    break;
   }
   case 'u':{
    return "U";
    break;
   }
   case 'v':{
    return "V";
    break;
   }
   case 'w':{
    return "W";
    break;
   }
   case 'x':{
    return "X";
    break;
   }
   case 'y':{
    return "Y";
    break;
   }
   case 'z':{
    return "Z";
    break;
   }
   }
  });
}

 

經測試javascript確實內建有toUpperCase(), toLowerCase() 方法,

哎,以前不知道有內建,到處找實現辦法,這個就是從網上搜集來的,確實可用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.