remove extra spaces with regexp

Discussion in 'Javascript' started by Bosconian, Aug 1, 2006.

  1. Bosconian

    Bosconian Guest

    I know that

    str.replace(/^\s+|\s+$/g,'');

    will trim a string of space, but what about removing extra spaces from the
    middle?

    Where

    "hello world"

    becomes

    "hello world"

    Thanks.
     
    Bosconian, Aug 1, 2006
    #1
    1. Advertising

  2. Bosconian

    Evertjan. Guest

    Bosconian wrote on 01 aug 2006 in comp.lang.javascript:

    > I know that
    >
    > str.replace(/^\s+|\s+$/g,'');
    >
    > will trim a string of space, but what about removing extra spaces from
    > the middle?


    [white space \s is more than spaces]

    > Where
    >
    > "hello world"
    >
    > becomes
    >
    > "hello world"


    result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Aug 1, 2006
    #2
    1. Advertising

  3. Bosconian wrote:
    > I know that
    >
    > str.replace(/^\s+|\s+$/g,'');
    >
    > will trim a string of space, but what about removing extra spaces from the
    > middle?
    >
    > Where
    >
    > "hello world"
    >
    > becomes
    >
    > "hello world"


    But how are you defining an "extra space"?

    Probably you would want to replace and continuous sequence of two or
    more spaces with a single space.

    Richard.
     
    Richard Cornford, Aug 1, 2006
    #3
  4. Bosconian

    Bosconian Guest

    "Evertjan." <> wrote in message
    news:Xns9812C83372239eejj99@194.109.133.242...
    > Bosconian wrote on 01 aug 2006 in comp.lang.javascript:
    >
    >> I know that
    >>
    >> str.replace(/^\s+|\s+$/g,'');
    >>
    >> will trim a string of space, but what about removing extra spaces from
    >> the middle?

    >
    > [white space \s is more than spaces]
    >
    >> Where
    >>
    >> "hello world"
    >>
    >> becomes
    >>
    >> "hello world"

    >
    > result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
    >
    >
    >
    > --
    > Evertjan.
    > The Netherlands.
    > (Please change the x'es to dots in my emailaddress)


    Evertjan, that's it--thanks! I just learned something new.
     
    Bosconian, Aug 1, 2006
    #4
  5. Bosconian

    Bosconian Guest

    "Richard Cornford" <> wrote in message
    news:...
    > Bosconian wrote:
    >> I know that
    >>
    >> str.replace(/^\s+|\s+$/g,'');
    >>
    >> will trim a string of space, but what about removing extra spaces from
    >> the
    >> middle?
    >>
    >> Where
    >>
    >> "hello world"
    >>
    >> becomes
    >>
    >> "hello world"

    >
    > But how are you defining an "extra space"?
    >
    > Probably you would want to replace and continuous sequence of two or
    > more spaces with a single space.
    >
    > Richard.
    >


    Yes, as you described. The syntax that Evertjan provided suits my needs
    exactly.
     
    Bosconian, Aug 1, 2006
    #5
  6. JRS: In article <Xns9812C83372239eejj99@194.109.133.242>, dated Tue, 1
    Aug 2006 17:40:49 remote, seen in news:comp.lang.javascript, Evertjan.
    <> posted :
    >Bosconian wrote on 01 aug 2006 in comp.lang.javascript:
    >
    >> I know that
    >>
    >> str.replace(/^\s+|\s+$/g,'');
    >>
    >> will trim a string of space, but what about removing extra spaces from
    >> the middle?

    >
    >[white space \s is more than spaces]
    >
    >> Where
    >>
    >> "hello world"
    >>
    >> becomes
    >>
    >> "hello world"

    >
    >result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');


    That compresses all multiple whitespace, including newlines. Within a
    RegExp, a space is a perfectly good character, standing for itself.

    {And if one wants a visible break in a RegExp (as literal or string)
    ISTM that escaping a real space with a \ should (but doesn't) do it.]


    Try .replace(/ +/g, ' ')
    or .replace(/[ \t]+/g, ' ')


    One might also want to remove residual single leading and trailing
    spaces on each line.

    <FAQENTRY>

    ISTM that FAQ 4.16 is *technically* wrong, in that ASCII should read
    UniCode.

    More importantly, ISTM that it should mention that \s includes both
    horizontal and vertical whitespace; the latter may sometimes be better
    untrimmed.
    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr John Stockton, Aug 1, 2006
    #6
  7. Bosconian wrote:
    > Richard Cornford wrote:
    >> Bosconian wrote:
    >>> I know that
    >>>
    >>> str.replace(/^\s+|\s+$/g,'');
    >>>
    >>> will trim a string of space, but what about removing
    >>> extra spaces from the middle?
    >>>
    >>> Where
    >>>
    >>> "hello world"
    >>>
    >>> becomes
    >>>
    >>> "hello world"

    >>
    >> But how are you defining an "extra space"?
    >>
    >> Probably you would want to replace and continuous sequence

    ^^^
    any
    >> of two or more spaces with a single space.

    >
    > Yes, as you described. The syntax that Evertjan provided
    > suits my needs exactly.


    Those two statements cannot both be true. Evertjan's regular expression
    does not do what I described.

    Richard.
     
    Richard Cornford, Aug 2, 2006
    #7
  8. Bosconian

    Bosconian Guest

    "Richard Cornford" <> wrote in message
    news:eapkut$khe$1$...
    > Bosconian wrote:
    >> Richard Cornford wrote:
    >>> Bosconian wrote:
    >>>> I know that
    >>>>
    >>>> str.replace(/^\s+|\s+$/g,'');
    >>>>
    >>>> will trim a string of space, but what about removing
    >>>> extra spaces from the middle?
    >>>>
    >>>> Where
    >>>>
    >>>> "hello world"
    >>>>
    >>>> becomes
    >>>>
    >>>> "hello world"
    >>>
    >>> But how are you defining an "extra space"?
    >>>
    >>> Probably you would want to replace and continuous sequence

    > ^^^
    > any
    >>> of two or more spaces with a single space.

    >>
    >> Yes, as you described. The syntax that Evertjan provided
    >> suits my needs exactly.

    >
    > Those two statements cannot both be true. Evertjan's regular expression
    > does not do what I described.
    >
    > Richard.
    >
    >


    Are you talking about

    result = str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');

    My test produced "hello world" from " hello world ".
     
    Bosconian, Aug 3, 2006
    #8
    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. Arifi Koseoglu
    Replies:
    2
    Views:
    983
    Arifi Koseoglu
    Apr 13, 2004
  2. mathieu
    Replies:
    3
    Views:
    600
    Bo Persson
    Sep 4, 2009
  3. eagle

    Grids, dropdown, sql queries and extra spaces

    eagle, Aug 22, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    146
    eagle
    Aug 23, 2005
  4. Joao Silva
    Replies:
    16
    Views:
    366
    7stud --
    Aug 21, 2009
  5. VK
    Replies:
    3
    Views:
    103
    Thomas 'PointedEars' Lahn
    May 25, 2006
Loading...

Share This Page