Archive for February 19, 2008

trim in Javascript!

So, Javascript has no trim option. Its truE!!
So here is two quick workarounds,

var trimmed = str.replace(/^\s+|\s+$/g, ”) ;

Thanks to nicknettleton!

Second one is,

// implementing a trim function for strings in javascript
<script language=”JavaScript” type=”text/javascript”>
<!–
String.prototype.trim = function () {
return this.replace(/^\s*/, “”).replace(/\s*$/, “”);
}var s = new String(” Hello “);
// use it like this
s=s.trim();
alert(“!” + s + “!”);
// end hiding contents –> </script>

Thanks to this site!

Comments (1)

fakibazi!

busy as hell! no time to chat! gtg.. hehehe

Comments (1)