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());