A robust way to remove white spaces (RegExp)

Discussion in 'Javascript' started by VK, May 22, 2006.

  1. VK

    VK Guest

    If it was already answered somewhere, I'll be glad to be pointed to
    (after the necessary comments on my search abilities :)

    I need as booletproof as possible way to strip out whitespaces from
    between tag borders in the source code.

    1) left border defined by gt sign >
    2) right border defined by lt sign <
    3) If the content between left and right borders consists only of white
    spaces it has to be removed.
    4) Content consists of white spaces only if it contains only \n, \r,
    \t, \f, space (\u0020) in any amount and any combinations.
    Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
    character.

    Thus say the outcome from:

    <foo>
    <bar>Foobar</bar>
    </foo>

    will be:

    <foo><bar>Foobar</bar></foo>

    Anyone knows of a similar RegExp?
    VK, May 22, 2006
    #1
    1. Advertising

  2. VK

    Evertjan. Guest

    VK wrote on 22 mei 2006 in comp.lang.javascript:

    > If it was already answered somewhere, I'll be glad to be pointed to
    > (after the necessary comments on my search abilities :)
    >
    > I need as booletproof as possible way to strip out whitespaces from
    > between tag borders in the source code.
    >
    > 1) left border defined by gt sign >
    > 2) right border defined by lt sign <
    > 3) If the content between left and right borders consists only of white
    > spaces it has to be removed.
    > 4) Content consists of white spaces only if it contains only \n, \r,
    > \t, \f, space (\u0020) in any amount and any combinations.
    > Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
    > character.
    >
    > Thus say the outcome from:
    >
    > <foo>
    > <bar>Foobar</bar>
    > </foo>
    >
    > will be:
    >
    > <foo><bar>Foobar</bar></foo>
    >
    > Anyone knows of a similar RegExp?
    >
    >


    myResult = myString.replace(/\s/g,'')


    \s is equivalent to [ \f\n\r\t\v]


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

  3. JRS: In article <>
    , dated Mon, 22 May 2006 07:23:40 remote, seen in
    news:comp.lang.javascript, VK <> posted :
    >If it was already answered somewhere, I'll be glad to be pointed to
    >(after the necessary comments on my search abilities :)
    >
    >I need as booletproof as possible way to strip out whitespaces from
    >between tag borders in the source code.
    >
    >1) left border defined by gt sign >
    >2) right border defined by lt sign <
    >3) If the content between left and right borders consists only of white
    >spaces it has to be removed.
    >4) Content consists of white spaces only if it contains only \n, \r,
    >\t, \f, space (\u0020) in any amount and any combinations.
    >Note: NON-BREAKING SPACE (nbsp, \u00A0) is /not/ a whitespace
    >character.
    >
    >Thus say the outcome from:
    >
    ><foo>
    > <bar>Foobar</bar>
    ></foo>
    >
    >will be:
    >
    ><foo><bar>Foobar</bar></foo>
    >
    >Anyone knows of a similar RegExp?


    Seems easy

    S = "<foo>\n <bar>Foobar</bar>\n</foo>"

    alert(S.replace(/>\s+</g, "><"))

    Note : \u0020 is not whitespace, but it is a representation of unit
    whitespace in common Unicode characters.

    --
    © 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, May 22, 2006
    #3
  4. Dr John Stockton wrote:

    > Note : \u0020 is not whitespace, but it is a representation of unit
    > whitespace in common Unicode characters.


    It is the Unicode escape sequence representation of _one_ Unicode SPACE
    character (U+0020).


    PointedEars
    --
    When you have eliminated all which is impossible, then
    whatever remains, however improbable, must be the truth.
    -- Sherlock Holmes in Sir Arthur Conan Doyle's
    "The Blanched Soldier"
    Thomas 'PointedEars' Lahn, May 25, 2006
    #4
    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. \Dandy\ Randy
    Replies:
    2
    Views:
    1,529
    Eric J. Roode
    Jul 21, 2003
  2. Niels Dybdahl

    Color.white vs. Color.WHITE

    Niels Dybdahl, Oct 6, 2004, in forum: Java
    Replies:
    3
    Views:
    425
    Chris Smith
    Oct 6, 2004
  3. Ben C
    Replies:
    6
    Views:
    2,133
    Leif K-Brooks
    Jan 28, 2007
  4. Joao Silva
    Replies:
    16
    Views:
    342
    7stud --
    Aug 21, 2009
  5. Bosconian

    remove extra spaces with regexp

    Bosconian, Aug 1, 2006, in forum: Javascript
    Replies:
    7
    Views:
    133
    Bosconian
    Aug 3, 2006
Loading...

Share This Page