Trim a string in Javascript.

By | March 29, 2012

Below is a simple function to implement this.

function trim(str)
{
        if(!str || typeof str != 'string')
        {
            return null;
        }
        return str.replace(/^[s]+/,'').replace(/[s]+$/,'').replace(/[s]{2,}/,' ');
}
Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *