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

F

FAQ server

-----------------------------------------------------------------------
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/d...html/2380d458-3366-402b-996c-9363906a7353.asp

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

http://www.informatics.sussex.ac.uk/courses/it/tutorials/nsJavaScriptRef/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.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected].
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. */
 
P

Peter Michaux

-----------------------------------------------------------------------
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/default.asp?url=/library/en-us/scri...

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

http://www.informatics.sussex.ac.uk/courses/it/tutorials/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top