Search This Blog

Thursday, April 16, 2009

Trimming the string using javascript

// Removes leading white spaces

function ltrim( value ){
var re = /\s*((\S+\s*)*)/;
return value.replace(re, "$1");
}

// Removes ending white spaces

function rtrim( value ) {
var re = /((\s*\S+)*)\s*/;
return value.replace(re, "$1");
}

// Removes leading and ending white spaces

function trim( value ){
return ltrim(rtrim(value));
}

No comments:

Post a Comment