<body onload="test()">
<script>
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function test() {
var str = " a ";
alert("Original string: '" + str + "'");
str = str.trim();
alert("Incompletely stripped string: '" + str + "'");
}
</script>
reference
http://www.bigbold.com/snippets/posts/show/701