Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail From: "J.R." Newsgroups: comp.lang.javascript Subject: Re: FAQ Topic - How do I trim whitespace? (2011-10-14) Date: Thu, 13 Oct 2011 22:09:37 -0300 Organization: Aioe.org NNTP Server Lines: 33 Message-ID: References: <4e976d84$0$289$14726298@news.sunsite.dk> NNTP-Posting-Host: edb3lPNLwDIT/BKKc/Xuzw.user.speranza.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.2.17) Gecko/20110414 Thunderbird/3.1.10 X-Notice: Filtered by postfilter v. 0.8.2 Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:7372 On 13/10/2011 20:00, 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. -- Joao Rodrigues (J.R.)