js基础6 — 字符串基本操作

1.将 'yw-input-color' 转为 'ywInputColor' 及 将 'ywInputColor' 转为 'yw-input-color' 

  var str = 'yw-input-color';
  String.prototype.showHump = function(){
    var arr = this.split('-');
    for (var i=1; i<arr.length; i++) {
      arr[i] = arr[i].split('')[0].toUpperCase() + arr[i].slice(1);
    }
    return arr.join('');
  }
  console.log(str.showHump());



  var str2 = 'ywInputColor';
  String.prototype.showLine = function(){
      var pattern = /^[A-Z]$/;
      var arr = this.split('');
      var arr2 = [];
      for (var i=0; i<arr.length; i++) {
        if (pattern.test(arr[i])) {
          arr[i] = '-' + arr[i].toLowerCase();
        }
        arr2.push(arr[i]);
      }
      return arr2.join('');
  }
  console.log(str2.showLine());

 

posted @ 2019-03-24 17:30  PopeyeSailorMan  阅读(197)  评论(0编辑  收藏  举报