Sunday, October 21, 2007

Javascript - trim() method...

Javascript is very cool in the way that i handles objects. The way that it lets you dynamically extend objects with properties and methodes, I find very cool.

Yes you need to keep focus due to this "anercistic" structure of javascript - sometimes you hate it but most of the times you probertly love it :-)

Anyway, I found a good example of this object extending feature. A trim method is not included in the standard javascript String object. So here you are - a way to extend String with a Regular Expression based trim() method:

String.prototype.trim = function() {
return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

(I found it here: Codeproject)

/Sten

No comments: