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

Chetan Patel

Chetan Patel is blogger, writes on Tech News, current trends, Gadgets Reviews, Website owner guide and Tips. You can connect him at Google+ & Facebook.