*********************************************** *** Javascript Trim Functions *** *** Provided by: http://www.breakingpar.com *** *********************************************** // ////////////////////////// // Supports Older Browsers // // ////////////////////////// result = trim(inputString); function trim(inputString) { // Removes leading and trailing spaces from the passed string. Also removes // consecutive spaces and replaces it with one space. If something besides // a string is passed in (null, custom object, etc.) then return the input. if (typeof inputString != "string") { return inputString; } var retValue = inputString; var ch = retValue.substring(0, 1); while (ch == " ") { // Check for spaces at the beginning of the string retValue = retValue.substring(1, retValue.length); ch = retValue.substring(0, 1); } ch = retValue.substring(retValue.length-1, retValue.length); while (ch == " ") { // Check for spaces at the end of the string retValue = retValue.substring(0, retValue.length-1); ch = retValue.substring(retValue.length-1, retValue.length); } while (retValue.indexOf(" ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ")+1, retValue.length); // Again, there are two spaces in each of the strings } return retValue; // Return the trimmed string back to the user } // Ends the "trim" function // /////////////////////////////////////////////////// // Supports JavaScript 1.2+ Browsers // // charCodeAt()" function is part of JavaScript 1.2 // // /////////////////////////////////////////////////// function Trim(Untrimmed) { /*This function removes all spaces from a string Example Trim(' Me And You ') returns 'MeAndYou' */ var Trimmed = '' for (var i = 0; i < Untrimmed.length; i++) { if (Untrimmed.charCodeAt(i)!=32) { Trimmed += Untrimmed[i] } } return Trimmed } // //////////////////////////////// // Regular Expressions Example 1 // // //////////////////////////////// //First, leading and trailing spaces are removed from the string in a couple //of statements. Build a regular expression statement that will find one or //more starting white space characters (space, tab, form feed, or line feed), //then any character (\W\w ends up being anything), then one or more ending //white spaces after a word boundary. If that pattern is found in the input //string, maintain only the middle characters (the \W\w part). //To trim interior consecutive spaces, set up the regular expression object //to hold two spaces (var obj = / /g; has 2 spaces). While 2 spaces is somewhere //in the string, replace all consecutive spaces with a single space. This only //does one pass through, so if there were 3 consecutive spaces, it would replace //the first 2 with a single space and end up with 2 spaces still. So the while //loop is necessary. //Note that if you only want to trim the leading and trailing spaces //(and not get rid of interior consecutive spaces), you can get rid //of the second instance where "obj" is set, and the while loop. function trim(value) { var temp = value; var obj = /^(\s*)([\W\w]*)(\b\s*$)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } var obj = / /g; while (temp.match(obj)) { temp = temp.replace(obj, " "); } return temp; } // //////////////////////////////// // Regular Expressions Example 2 // // //////////////////////////////// //Another way to accomplish the replacing of interior multiple spaces //is to replace all instances of one or more spaces with a single space. //So if there already is one space, it will be replaced with one space. //But if there are 2 or more consecutive spaces, all will be replaced with //1 space. This eliminates the while loop. However, if the string is just //a line of spaces, then this results in a single space, so an additional //check needs to be made. Using this method, the function becomes: function trim(value) { var temp = value; var obj = /^(\s*)([\W\w]*)(\b\s*$)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } var obj = / +/g; temp = temp.replace(obj, " "); if (temp == " ") { temp = ""; } return temp; }