Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <1543388.qVoOGUtdWV@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 14 Oct 2011 20:54:16 +0200 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: FAQ Topic - How do I trim whitespace? (2011-10-14) Newsgroups: comp.lang.javascript References: <4e976d84$0$289$14726298@news.sunsite.dk> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 78 NNTP-Posting-Date: 14 Oct 2011 20:54:17 CEST NNTP-Posting-Host: d4d55906.newsspool4.arcor-online.net X-Trace: DXC=V]Y`ToW0W3@gj[ZPFj7ehO4IUK FAQ server wrote: >> ----------------------------------------------------------------------- >> FAQ Topic - How do I trim whitespace? >> ----------------------------------------------------------------------- >> >> ECMAScript 5 defines `String.prototype.trim`. Where not supported, >> it can be added as a function that uses a regular expression: >> >> if(!String.prototype.trim) { >> String.prototype.trim = function() { >> return String(this).replace(/^\s+|\s+$/g, ""); >> }; >> } > >> The complete comp.lang.javascript FAQ is at >> http://jibbering.com/faq/ >> > > A few months ago, there was an article at Jibbering,com with a different > version of this code: > > /** @see http://jibbering.com/faq/#trimString */ > if (typeof String.prototype.trim !== "function") { > String.prototype.trim = function () { > // return String(this).replace(/^\s+|\s+$/g, ""); > return this.replace(/^\s+|\s+$/g, ""); > }; > } > > I think that is more consistent with ECMA-262 5th ed., 15.5.4.20. `!x' requires RETURN Not(ToBoolean(GetValue(x))); `typeof x !== y' requires lref := FUNCTION(x) { val := GetValue(x); IF Type(val) = Reference { IF IsUnresolvableReference(v) { RETURN "undefined"; } val := GetValue(val); } RETURN GetResultByType(Type(val)); }(x); lval := GetValue(lref); rref := Evaluate(y); rval := GetValue(rref); r := (rval === lval); /* 11.9.6 */ RETURN Not(r); Implicit type conversion may suffice, especially iff it is more efficient than `typeof' (benchmarks anyone?); it is not logical to assume that the property would be assigned a non-function true-value, or a host object reference. Regardless, strict comparison is pointless here. `typeof' is not going to result in a non-string. Also, we are at ES 5.1 (2011-06) by now, and section 15.5.4.20 says basically what the line does that is commented out here (there are implementations that do not implement whitespace as specified in ECMAScript, but it appears prudent that whitespace be handled either implementation- independent or implementation-dependent in all places in a script library). PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from (404-comp.)