<div class="codetitle"> <span><a style="CURSOR: pointer" data="34316" class="copybut" id="copybut34316" onclick="doCopy('code34316')"><u>複製代碼</u></a></span> 代碼如下:</div> <div class="codebody" id="code34316"> <br><span style="FONT-SIZE: 18px"> <br> <br> <br><meta charset="utf-8"> <br><title>字串替換</title> <br><script type="text/ javascript"> <BR>window.onload = function(){ <BR>var str='border-bottom-color'; <BR>// String.prototype.transform = function(){ <BR>// var arr = this.split('-'); <BR>// for(var i=1;i<arr.length;i ){ <BR>// arr[i]=arr[i].charAt(0) .toUpperCase() arr[i].substring(1); <BR>// } <BR>// return arr.join('') <BR>// } <BR>// alert(str.transform( )); 物件導向加普通string方法<BR>//物件導向以正規表示方式<BR>String.prototype.transform = function(){ <BR>var re=/-(w)/g; <BR> return this.replace(re,function(){ <BR>var args=arguments; <BR>return args[1].toUpperCase(); <BR>}) <BR>} <BR>alert(str.transform( )); <br><br>} <BR></script> <br> <br> <br><h3>寫一個字串轉成駝峰的方法? <br> <br>例如 border-bottom-color->borderBottomColor <br> </h3> <br> <br></span> <br> </div>