trim, ltrim and rtrim in javascript





13
Date Submitted Mon. Sep. 25th, 2006 2:22 AM
Revision 1 of 1
Syntax Master sundaramkumar
Tags JavaScript
Comments 1 comments
Trim , Left trim (ltrim) and Right Trim (rtrim) in javascript


//Function to trim the space in the left side of the string
function ltrim ( s ){
  return s.replace( /^\s*/, "" );
}

//Function to trim the space in the right side of the string
function rtrim ( s ){
   return s.replace( /\s*$/, "" );
}

//*Function to trim the space in the  string
function trim(s) {
   var temp = s;
   return temp.replace(/^\s+/,'').replace(/\s+$/,'');
}



 
ltrim() will remove all the spaces to the left in a string rtrim() will remove all the spaces to the right in a string trim() will all the left , right and spaces inside the string Ex: var x = " thestring"; ltrim(x) will return "thestring"; var y = "thestring "; rtrim(y) will return "thestring"; var z = " the st ri ng "; trim(z) will return "thestring"

Comments

Comments But must it be regexes?
Tue. Sep. 26th, 2006 1:02 PM    Scripter sehrgut

Voting