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

Discussion in 'Javascript' started by FAQ server, Dec 25, 2007.

  1. FAQ server

    FAQ server Guest

    -----------------------------------------------------------------------
    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.
    FAQ server, Dec 25, 2007
    #1
    1. Advertising

  2. In comp.lang.javascript message <47704806$0$90263$.
    dk>, Tue, 25 Dec 2007 00:00:01, FAQ server <>
    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.
    Dr J R Stockton, Dec 26, 2007
    #2
    1. Advertising

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

    On Dec 24, 4:00 pm, "FAQ server" <> wrote:
    > -----------------------------------------------------------------------
    > 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
    Peter Michaux, Dec 26, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    123
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    1
    Views:
    107
    Dr J R Stockton
    Feb 29, 2008
  3. FAQ server
    Replies:
    0
    Views:
    98
    FAQ server
    May 1, 2008
  4. FAQ server
    Replies:
    2
    Views:
    97
    Dr J R Stockton
    Jun 30, 2008
  5. FAQ server
    Replies:
    1
    Views:
    117
    Dr J R Stockton
    Aug 28, 2008
Loading...

Share This Page