javascript的trim()函數的實現

來源:互聯網
上載者:User

javascript的trim()函數的實現

 

 

在JavaScript中我們需要用到trim的地方很多,但是JavaScript又沒有獨立的trim函數或者方法可以使用,所以我們需要自己寫個trim函數來實現我們的目的

比如方法一:

 

   String.prototype.trim= function(){ 

       // 用Regex將前後空格 

       // 用Null 字元串替代。 

       return this.replace(/(^\s*)|(\s*$)/g, ""); 

    }

 

方法二:

 

   function  trim(str){

       for(var  i  = 0  ;  i<str.length  && str.charAt(i)=="  "  ; i++  )  ;

       for(var  j  =str.length; j>0  &&  str.charAt(j-1)=="  " ;  j--)  ;

       if(i>j)  return  ""; 

       return  str.substring(i,j); 

    }

 

Javascript中S.replace(/(^\s*)|(\s*$)/g,"");的 (^\s*)|(\s*$) 是什麼意思的?

 

 

    首先是把/(^\s*)|(\s*$)/g替換為""

    然後,/.../g 裡面的,是表示放置萬用字元的地方,g代表全域參數,(^\s*)或者(\s*$)都將被替換為""

    匹配首尾空白字元的Regex:^\s*|\s*$ 可以用來刪除行首行尾的空白字元(包括空格、定位字元、換頁符等等),非常有用的運算式

 

 

 

javascript內實現trim的方法

 

直接拷貝的話,空格可能出現問題,請仔細檢查

 

function  trim(str)

{

for(var  i   =   0  ;   i<str.length   &&  str.charAt(i)=="  "   ;   i++  )   ;

for(var  j   =str.length;   j>0  &&   str.charAt(j-1)=="   "  ;   j--)   ;

if(i>j)  return   ""; 

return  str.substring(i,j); 

}

 

方法二:

//   增加一個名為   trim  的函數作為

//  String   建構函式的原型對象的一個方法。

String.prototype.trim   =  function()

{

        //   用Regex將前後空格

        //   用Null 字元串替代。

        return  this.replace(/(^\s*)|(\s*$)/g,  "");

}

 

//   有空格的字串

var  s   =   "         我的長度         ";

 

//   顯示trim前長度

window.alert(s   +  "   trim前長度:   ("  +   s.length   +  ")");

 

//   刪除前後空格

s  =   s.trim();

//   顯示trim後長度

window.alert(s   +  "     trim後長度:("   +  s.length   +   ")");

 

 

方法3:

 

//javascript中調用vbscript的函數,構造一個javascript版的trim 函數

 

<html>

<head>

</head>

<body>

 

<p> </p>

<script language=vbscript>

function VBTrimStr(temStr)

VBTrimStr=trim(temStr)

end function

</script>

 

<script language=javascript>

function TrimStr(temStr){

return VBTrimStr(temStr)

}

</script>

<form name=fmTest>

<input type=text name=txtTest>

<input type=button name=btnOkvalue=ok>

</form>

<script language=javascript for=btnOkevent=onclick>

var getStr=document.fmTest.txtTest.value

alert("*" + getStr +"*")

getStr=TrimStr(getStr)

alert("*" + getStr +"*")

</script>

</body>

</html>

 

Javascript Trim Member Functions

Use the code below to make trim a method ofall Strings. These are useful to place in a global Javascript file included byall your pages.

 

String.prototype.trim = function() {returnthis.replace(/^\s+|\s+$/g,"");}

String.prototype.ltrim = function() {returnthis.replace(/^\s+/,"");}

String.prototype.rtrim = function() {returnthis.replace(/\s+$/,"");}

// example of using trim, ltrim, andrtrimvar myString = " hello my name is ";

alert("*"+myString.trim()+"*");

alert("*"+myString.ltrim()+"*");

alert("*"+myString.rtrim()+"*");

 

 

Javascript Trim Stand-Alone Functions

If you prefer not to modify the stringprototype, then you can use the stand-alone functions below.

 

function trim(stringToTrim) {returnstringToTrim.replace(/^\s+|\s+$/g,"");}

function ltrim(stringToTrim) {returnstringToTrim.replace(/^\s+/,"");}

function rtrim(stringToTrim) {returnstringToTrim.replace(/\s+$/,"");}

// example of using trim, ltrim, andrtrimvar myString = " hello my name is ";

alert("*"+trim(myString)+"*");

alert("*"+ltrim(myString)+"*");

alert("*"+rtrim(myString)+"*");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.