zoukankan      html  css  js  c++  java
  • javascript

    Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.

    Examples:

    // returns "theStealthWarrior"
    console.log(toCamelCase("the-stealth-warrior"));
    
    // returns "TheStealthWarrior"
    console.log(toCamelCase("The_Stealth_Warrior"))
    

    去掉中间间隔符,并且从第二个单词开始首字母大写

    • my answer:
    function toCamelCase(str) {
      if (str == ''){
        return '';
      }
      var strnew = str.replace(/[^a-zA-Z]/g,'-');
      var a = strnew.split('-');
      for (var i = 1; i < a.length; i++) {    //如果从第一个单词开始首字母大写就改成 i=0;
        a[i] = a[i][0].toUpperCase()+a[i].slice(1);  //首字母大写
      }
      return a.join('');
    }
    
    • best answer:
    function toCamelCase(str){
          var regExp=/[-_]w/ig;
          return str.replace(regExp,function(match){
                return match.charAt(1).toUpperCase();
           });
    }
    
  • 相关阅读:
    画图软件
    万用表
    传导发射
    MOT
    Docker
    第十二章、私营部门和第三部门中的采购
    第十一章、公共部门中的采购
    第十章、部门与行业环境
    第九章、信息与通信技术系统
    第八章、组织的采购职能
  • 原文地址:https://www.cnblogs.com/kid2333/p/7498374.html
Copyright © 2011-2022 走看看