473,840 Members | 1,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.prototyp e.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototyp e.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototyp e.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.javas cript 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 5525
On Dec 24, 4:00 pm, "FAQ server" <javascr...@dot internet.bewrot e:
-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
-----------------------------------------------------------------------

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

String.prototyp e.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototyp e.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototyp e.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.javas cript message <47************ ***********@new s.sunsite.
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <ja********@dot internet.be>
posted:
>FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
>
Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototyp e.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.prototyp e.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.demo n.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...@dot internet.bewrot e:
>-----------------------------------------------------------------------
FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
-----------------------------------------------------------------------

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

String.prototyp e.trimLeft =
function()
{
return this.replace(/^\s+/,'');
}
String.prototyp e.trimRight =
function()
{
return this.replace(/\s+$/,'');
}
String.prototyp e.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.c om/group/comp.lang.javas cript/browse_thread/thread/c4114b310629e55 9/30f9f0254657adc b?lnk=gst&q=FAQ +Topic+Trim+Whi tespace#30f9f02 54657adcb>

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.javas cript 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.javas cript message <47************ ***********@new s.sunsite.
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <ja********@dot internet.be>
posted:
>FAQ Topic - How do I trim whitespace -
trim/trimRight/trimLeft
>Using Regular Expressions (JavaScript 1.2/JScript 3+) :

String.prototy pe.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.javas cript.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javas cript 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
1105
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
1
1174
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft ----------------------------------------------------------------------- Using Regular Expressions (JavaScript 1.2/JScript 3+) : String.prototype.trimLeft = function() {
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10598
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10299
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9436
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7836
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3136
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.