The first type: loop check replacement
//For users to callfunction trim(s){ return trimRight(trimLeft(s)); } //Remove the blank on the leftfunction trimLeft(s){ if(s == null) { return ""; } var whitespace = new String(" \t\n\r"); var str = new String(s); if (((0)) != -1) { var j=0, i = ; while (j < i && ((j)) != -1){ j++; } str = (j, i); } return str; } //Remove the blank on the rightfunction trimRight(s){ if(s == null) return ""; var whitespace = new String(" \t\n\r"); var str = new String(s); if (((-1)) != -1){ var i = - 1; while (i >= 0 && ((i)) != -1){ i--; } str = (0, i+1); } return str; }
The second type: regular replacement
<SCRIPT LANGUAGE="JavaScript"> <!-- = function() { return (/(^\s*)|(\s*$)/g, ""); } = function() { return (/(^\s*)/g, ""); } = function() { return (/(\s*$)/g, ""); } //--> </SCRIPT>
//Get the left space;function ltrim(s){ return (/(^\s*)/g, ""); } //Remove the right space;function rtrim(s){ return (/(\s*$)/g, ""); } //Get the left and right spaces;function trim(s){ return (/(^\s*)|(\s*$)/g, ""); }
The third type: use jquery
$.trim(str)
The internal implementation of jquery is:
function trim(str){ return (/^(\s|\u00A0)+/,'').replace(/(\s|\u00A0)+$/,''); }
Fourth: Use motools
function trim(str){ return (/^(\s|\xA0)+|(\s|\xA0)+$/g, ''); }
Fifth: Cropping strings
function trim(str){ str = (/^(\s|\u00A0)+/,''); for(var i=-1; i>=0; i--){ if(/\S/.test((i))){ str = (0, i+1); break; } } return str; }
//---------------------------------------------------------- // Remove the spaces before and after the string// Return value:// Remove the string after space//---------------------------------------------------------- function trim(param) { if ((vRet = param) == '') { return vRet; } while (true) { if ( (' ') == 0) { vRet = (1, parseInt()); } else if ((parseInt() != 0) && ( (' ') == parseInt() - 1)) { vRet = (0, parseInt() - 1); } else { return vRet; } } }
For more content, please refer to the article below.