Total Pageviews

2006/09/27

[JavaScript] How to do trim() in JavaScript

<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>

No comments: