First: cyclic check replacement
[JavaScript]
function return
Remove the left blank
functionTrimleft (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 on the right
functionTrimRight (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
[JavaScript]
<script language= "JavaScript" > <!-- String.prototype.Trim = Span style= "color: #0000ff;" >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>
The third type: using jquery
[JavaScript]
$.trim (str) jquery internal implementation is: [JavaScript] function return str.replace (/^ (\s|\u00a0) +/, "). Replace (/(\S|\U00A0) +$/, '
Fourth: Use of Motools
[JavaScript]
functionreturn str.replace (/^ (\s|\xa0) +| ( \S|\XA0) +$/g, "
Fifth: Clipping string mode
[JavaScript]
function= str.replace (/^ (\s|\u00a0) +/, 'for (var i=str.length-1; i>=0; i--if(/\s/= str.substring (0, i+1breakreturn str;}
Five ways Javascript removes whitespace before and after a string