JS trim


string.trim() delete spaces at the front and end of a string.

var x=" 342 ";
var y=x.trim();
alert("|" + y + "|"); //|342|

Add left trim or right trim to the String prototype:

String.prototype.trim = function()
{
	return this.replace(/^\s+|s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/s+$/,"");
}
var str=" time ";
alert("|" + str.ltrim() + "|"); //|time |

Trim more character except spaces:

String.prototype.trim = function()
{
	return this.replace(/^[<>\s]+|[<>\s]+$/g,"");
}
var str=" <a href> ";
alert("|" + str.trim() + "|"); //a href