X

Trim Javascript String : Remove space at starting or ending of string

You can do trim of any javascript string by below function.

function trim(str)
{
 return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

Example :

var triotips = " This is best site. ";
alert(trim(triotips));

Result :
This is best site

If you are looking for trimming each value of Array in javascript then see here. Trim Array in Javascript: Trim elements of an array in Javascript

This post was last modified on July 19, 2011 8:47 am

Categories: Javascript
Chetan Patel: Chetan Patel is blogger, writes on Tech News, current trends, Gadgets Reviews, Website owner guide and Tips. You can connect him at <a href="https://plus.google.com/118385579556796801405?rel=author">Google+</a> & <a href="https://www.facebook.com/patecj">Facebook</a>.