Re: FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft (2007-12-25)
- From: Dr J R Stockton <jrs@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 26 Dec 2007 12:33:35 +0000
In comp.lang.javascript message <47704806$0$90263$14726298@xxxxxxxxxxxxx
dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <javascript@xxxxxxxxxxxxxx>
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.
.
- Follow-Ups:
- References:
- Prev by Date: Free Scripts
- Next by Date: Re: Prototype & Scriptaculous are taking 1-3 seconds each to load - why?
- Previous by thread: FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft (2007-12-25)
- Next by thread: Re: FAQ Topic - How do I trim whitespace - trim/trimRight/trimLeft (2007-12-25)
- Index(es):
Relevant Pages
|
Loading