469,955 Members | 1,768 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft (2007-12-25)

-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim =
function()
{
return this.replace(/^\s+|\s+$/g,'');
}

http://docs.sun.com/source/816-6408-10/regexp.htm

http://msdn.microsoft.com/library/de...63906a7353.asp

http://en.wikipedia.org/wiki/Regular_expression

http://www.informatics.sussex.ac.uk/...f/contents.htm

http://www.merlyn.demon.co.uk/js-valid.htm
--
Postings such as this are automatically sent once a day. Their
goal is to answer repeated questions, and to offer the content to
the community for continuous evaluation/improvement. The complete
comp.lang.javascript FAQ is at http://jibbering.com/faq/index.html.
The FAQ workers are a group of volunteers. The sendings of these
daily posts are proficiently hosted by http://www.pair.com.

Dec 25 '07 #1
4 5333
On Dec 24, 4:00 pm, "FAQ server" <javascr...@dotinternet.bewrote:
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim =
function()
{
return this.replace(/^\s+|\s+$/g,'');
}

http://docs.sun.com/source/816-6408-10/regexp.htm

http://msdn.microsoft.com/library/de...ary/en-us/scri...

http://en.wikipedia.org/wiki/Regular_expression

http://www.informatics.sussex.ac.uk/...s/nsJavaScript...

http://www.merlyn.demon.co.uk/js-valid.htm

The last time I looked at this FAQ entry as a daily post I think it
still had the old non-regexp versions. I mentioned I thought it was a
bad idea to encourage augmenting built in prototypes. I still think is
the case.

Peter
Dec 26 '07 #2
In comp.lang.javascript message <47***********************@news.sunsite.
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <ja********@dotinternet.be>
posted:
>FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
>
Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
...

The FAQ is intended for incomers to read. Therefore, each Topic should
explicitly answer its Subject question. That one does not. It shows
three strange statements each containing a nameless function and
something moderately inscrutable in parentheses.

Also, though the Subject refers to whitespace the answer refers only to
leading/trailing whitespace.

NOTE that whitespace can, in some contexts such as Pascal code, include
newlines; the word used alone is thus not ideal for describing what trim
trims.

.. . .

Most string manipulations are best done by using Regular Expressions.

To reduce multiple spaces/tabs within a string in S1 to single spaces :
S2 = S1.replace(/\s{2,}/, " ") // /\s{2,}/ is a RegExp literal

One can give String Objects a Method to do that by :
String.prototype.unPad =
function() { return this.replace(/\s{2,}/, " ") }

/* then continue with the trim routines as before (though the first two
are not needed, being easily derived from an understanding of the third)
and adjust the Subject of the item. */

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Dec 26 '07 #3
Peter Michaux said the following on 12/26/2007 10:26 AM:
On Dec 24, 4:00 pm, "FAQ server" <javascr...@dotinternet.bewrote:
>-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
-----------------------------------------------------------------------

Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototype.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototype.trim =
function()
{
return this.replace(/^\s+|\s+$/g,'');
}

http://docs.sun.com/source/816-6408-10/regexp.htm

http://msdn.microsoft.com/library/de...ary/en-us/scri...

http://en.wikipedia.org/wiki/Regular_expression

http://www.informatics.sussex.ac.uk/...s/nsJavaScript...

http://www.merlyn.demon.co.uk/js-valid.htm


The last time I looked at this FAQ entry as a daily post I think it
still had the old non-regexp versions.
Yes, and that thread is what caused me to remove them.

<URL:
http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/c4114b310629e559/30f9f0254657adcb?lnk=gst&q=FAQ+Topic+Trim+Whitespa ce#30f9f0254657adcb>

When I posted that I had removed them, there were no comments about me
moving them. At the time of that thread, I was in the middle of
re-writing all of the code in the FAQ and got side-tracked on it with
the Holidays.
I mentioned I thought it was a bad idea to encourage augmenting built in prototypes.
I still think is the case.
Agreed.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
FAQ Notes: http://www.jibbering.com/faq/faq_notes/faq_notes.html
ECMAScript Language Specification via FAQ2.6
Dec 27 '07 #4
Dr J R Stockton said the following on 12/26/2007 7:33 AM:
In comp.lang.javascript message <47***********************@news.sunsite.
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <ja********@dotinternet.be>
posted:
>FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
>Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototype.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
...


The FAQ is intended for incomers to read. Therefore, each Topic should
explicitly answer its Subject question. That one does not. It shows
three strange statements each containing a nameless function and
something moderately inscrutable in parentheses.
It almost looks like the code on your site. With the exception of
whitespace. And the white space was added upon request.
Also, though the Subject refers to whitespace the answer refers only to
leading/trailing whitespace.
And 99% of the time, that is what people want.
NOTE that whitespace can, in some contexts such as Pascal code, include
newlines; the word used alone is thus not ideal for describing what trim
trims.
What happens in Pascal, Delphi, C, C++, PHP or any other language is
irrelevant to what happens in JS, and especially in an FAQ about JS.
Unless you think that people wanting to learn Pascal would attempt to do
so by reading an FAQ for comp.lang.javascript.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Dec 27 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.