First: cyclic check replacement//for the user to invokefunctionTrim (s) {returnTrimRight (Trimleft (s));} //Remove the left blankfunctionTrimleft (s) {if(s = =NULL) { return""; } varwhitespace =NewString ("\t\n\r"); varstr =NewString (s); if(Whitespace.indexof (Str.charat (0))! =-1) { varj=0, i =str.length; while(J < i && Whitespace.indexof (Str.charat (j))! =-1) {J++; } STR=Str.substring (J, I); } returnstr;} //Remove the blank www.jb51.net on the rightfunctionTrimRight (s) {if(s = =NULL)return""; varwhitespace =NewString ("\t\n\r"); varstr =NewString (s); if(Whitespace.indexof (Str.charat (str.length-1))! =-1){ vari = Str.length-1; while(I >= 0 && whitespace.indexof (str.charat (i))! =-1) {i--; } STR= str.substring (0, i+1); } returnstr;}
the second type: regular replacement<script language= "JavaScript" > <!--String.prototype.Trim=function() { return This. Replace (/(^\s*) | ( \s*$)/g, ""); } String.prototype.LTrim=function() { return This. Replace (/(^\s*)/g, ""); } String.prototype.RTrim=function() { return This. Replace (/(\s*$)/g, ""); } // -</SCRIPT>//go left space;functionLTrim (s) {returnS.replace (/(^\s*)/g, "");}//go to the right space;functionRTrim (s) {returnS.replace (/(\s*$)/g, "");}//to the left and right spaces;functionTrim (s) {returnS.replace (/(^\s*) | ( \s*$)/g, "");}
the third type: Using Jquery$.trim (str) jquery internal implementation is:functionTrim (str) {returnStr.replace (/^ (\s|\u00a0) +/, '). Replace (/(\S|\U00A0) +$/, '); } The fourth type: using MotoolsfunctionTrim (str) {returnStr.replace (/^ (\s|\xa0) +| ( \S|\XA0) +$/g, "); } Fifth: clipping string modefunctionTrim (str) {str= Str.replace (/^ (\s|\u00a0) +/, "); for(varI=str.length-1; i>=0; i--){ if(/\s/. Test (Str.charat (i))) {STR= str.substring (0, i+1); Break; } } returnstr; }
// 去掉字符串前后的空格
// 返回值:
// 去除空格后的字符串
//----------------------------------------------------------
function
trim(param) {
if
((vRet = param) ==
‘‘
) {
return
vRet; }
while
(
true
) {
if
(vRet.indexOf (
‘ ‘
) == 0) {
vRet = vRet.substring(1, parseInt(vRet.length));
}
else
if
((parseInt(vRet.length) != 0) && (vRet.lastIndexOf (
‘ ‘
) == parseInt(vRet.length) - 1)) {
vRet = vRet.substring(0, parseInt(vRet.length) - 1);
}
else
{
return
vRet;
}
}
}
Javascript 5 ways to filter delete before and after all spaces