Tuesday, 31 August 2010

JavaScript trim functions

These are from: Shailesh N. Humbad, and they are really rather cool, check out his site.

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
  return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
  return stringToTrim.replace(/\s+$/,"");
}