473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 5489
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.