Thursday 5 November 2009

Java script trim function

I have used this trick in different places and sometimes when I need it again, I always forget, tahts why I am putting it here:

function AllTrim(str) // call this function if you want to remove spaces from both sides
{
str = LTrim(str);
str = RTrim(str);
return str;
}
function LTrim(str) // this function removes spaces from left
{
return str.replace(/^\s+/g,'');
}
function RTrim(str) // this function removes spaces from right
{
return str.replace(/\s+$/g,'');
}

No comments: