RegEx to remove spaces

Discussion in 'Javascript' started by Dante, Mar 4, 2004.

  1. Dante

    Dante Guest

    I need a regular expression to remove all spaces from a string.
    The best I could do was this:
    var spacefix = / /gi;
    var out = data.replace(spacefix,"");

    Thanks so much, Dante.
    Dante, Mar 4, 2004
    #1
    1. Advertising

  2. \s strips spaces, tabs, and line break characters. You can do /[ \t]/g if
    you just want to kill nonbreaking spaces.

    The easiest thing you could do would probably be:

    String.prototype.stripSpaces = function( ){ return this.replace( /\s/g, "" ); };

    So "H e l l o W o r l d".stripSpaces( ); would behave similarly to
    ..toUppercase.

    For the sake of sake, this is an equally viable function:

    String.prototype.strip = function( exp ){ return this.replace(exp?exp:/\s/g,""); };

    With this, "H e l l o".strip(); would render "Hello", and "Hello".strip(
    /[o]/ ); would render "Hell".

    Whatever thou prefereth.

    ..Brian
    Brian Kerrick Nickel, Mar 4, 2004
    #2
    1. Advertising

  3. Dante

    Dante Guest

    IE5 has trouble with \s. Oh well.

    Also I was wondering how I can write a regex that matches only words
    and numbers, not spaces.

    I was thinking something with [:alpha:].

    Brian, you will receive credit for the Word Counter Script I am
    fixing.
    Dante, Mar 5, 2004
    #3
  4. Dante wrote:

    > IE5 has trouble with \s. Oh well.


    Mine have not.

    Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0; Q312461)
    Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; Q312461)

    Testcase:

    javascript:document.write(" bl blubb".replace(/\s/g, "") +
    "<br>\n"); document.write(navigator.userAgent)

    > Also I was wondering how I can write a regex that matches only words
    > and numbers, not spaces.
    >
    > I was thinking something with [:alpha:].


    /\w/g

    Get informed about the RegExp object:
    <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136>


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 9, 2004
    #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. John B. Matthews
    Replies:
    4
    Views:
    655
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    613
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,568
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    862
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,848
    Mayeul
    Dec 10, 2009
Loading...

Share This Page