JavaScript Trim, LTrim, RTrim Functions using Regular Expressions

Tuesday, July 28, 2009

function jsTrim(stringToTrim)
{
return stringToTrim.replace(/^\s+\s+$/g,"");
}

function jsLtrim(stringToTrim)
{
return stringToTrim.replace(/^\s+/,"");
}

function jsRtrim(stringToTrim)
{
return stringToTrim.replace(/\s+$/,"");
}


Usage:
alert(jsTrim("     Webpage Tips     "));
alert(jsLtrim("     Webpage Tips"));
alert(jsRtrim("Webpage Tips     "));

0 comments:

  © Blogger templates Newspaper by Ourblogtemplates.com 2008

Back to TOP