Thursday, June 5, 2008

String Trim() in Javascript

<script language="javascript">
String.prototype.trim=function()
{
 return this.replace(/^\s*|\s*$/g, '');
}
String.prototype.ltrim=function()
{
 return this.replace(/^\s*/g, '');
}
String.prototype.rtrim=function()
{
 return this.replace(/\s*$/g, '');
}
var sf = " This is TEST message   ";
alert(sf.trim());
</script>

1 comment:

Rochak Chauhan said...

You can also use
http://rochakchauhan.com/blog/2008/10/10/rochakjs-javascript-class-of-common-functions/
It contains many commonly needed functions for JavaScript including trim();