Replace all strings

Discussion in 'Javascript' started by Archos, Feb 22, 2012.

  1. Archos

    Archos Guest

    How to replace all strings at the same time?

    >>> var a = "a,b,c,d";
    >>> a.toString().replace(",", "")

    "ab,c,d"
     
    Archos, Feb 22, 2012
    #1
    1. Advertising

  2. Archos

    J.R. Guest

    On 22/02/2012 15:48, Archos wrote:
    > How to replace all strings at the same time?
    >
    >>>> var a = "a,b,c,d";
    >>>> a.toString().replace(",", "")

    > "ab,c,d"


    If I understood correctly, you want to replace all the commas in a
    variable with '', right? Then you might use a RegExp such as:

    var a = "a,b,c,d";
    a.replace(/,/g, '')

    --
    Joao Rodrigues (J.R.)
     
    J.R., Feb 22, 2012
    #2
    1. Advertising

  3. Archos

    Archos Guest

    On Feb 22, 6:48 pm, "J.R." <> wrote:
    > On 22/02/2012 15:48, Archos wrote:
    >
    > > How to replace all strings at the same time?

    >
    > >>>> var a = "a,b,c,d";
    > >>>> a.toString().replace(",", "")

    > > "ab,c,d"

    >
    > If I understood correctly, you want to replace all the commas in a
    > variable with '', right? Then you might use a RegExp such as:
    >
    > var a = "a,b,c,d";
    > a.replace(/,/g, '')
    >
    > --
    > Joao Rodrigues (J.R.)


    Yes, thanks. I hope that the use of regular expressions been right for
    main browsers.
     
    Archos, Feb 22, 2012
    #3
  4. Archos

    Evertjan. Guest

    J.R. wrote on 22 feb 2012 in comp.lang.javascript:

    > On 22/02/2012 15:48, Archos wrote:
    >> How to replace all strings at the same time?
    >>
    >>>>> var a = "a,b,c,d";
    >>>>> a.toString().replace(",", "")

    >> "ab,c,d"

    >
    > If I understood correctly, you want to replace all the commas in a
    > variable with '', right? Then you might use a RegExp such as:
    >
    > var a = "a,b,c,d";
    > a.replace(/,/g, '')
    >


    var a = "a,b,c,d";
    a = a.replace(/,/g, '');

    ==============

    If you don't feel comfortable with Regex, try:

    var a = "a,b,c,d";
    a = a.split(',').join('');



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 22, 2012
    #4
  5. Archos

    J.R. Guest

    On 22/02/2012 17:29, Archos wrote:
    > On Feb 22, 6:48 pm, "J.R."<> wrote:
    >> On 22/02/2012 15:48, Archos wrote:
    >>
    >>> How to replace all strings at the same time?

    >>
    >>>>>> var a = "a,b,c,d";
    >>>>>> a.toString().replace(",", "")
    >>> "ab,c,d"

    >>
    >> If I understood correctly, you want to replace all the commas in a
    >> variable with '', right? Then you might use a RegExp such as:
    >>
    >> var a = "a,b,c,d";
    >> a.replace(/,/g, '')
    >>

    >
    > Yes, thanks. I hope that the use of regular expressions been right for
    > main browsers.


    Hi,
    The "searchValue" argument of the String.prototype.replace method can be
    a regular expression or not, according to the standards (ECMA-262
    Editions 3 and 5, section 15.5.4.11).

    Cheers,
    Joao Rodrigues (J.R.)
     
    J.R., Feb 22, 2012
    #5
  6. On 22 Feb 2012 22:02:45 GMT, "Evertjan."
    <> wrote:

    [snip]

    >If you don't feel comfortable with Regex, try:
    >
    >var a = "a,b,c,d";
    >a = a.split(',').join('');


    Neat! <added to arsenal>

    Sincerely,

    Gene Wirchenko
     
    Gene Wirchenko, Feb 22, 2012
    #6
    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. Xah Lee
    Replies:
    1
    Views:
    1,422
    YYusenet
    Jan 31, 2005
  2. Brian Blais
    Replies:
    1
    Views:
    400
    Bruno Desthuilliers
    Jun 27, 2006
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    802
    Malcolm
    Jun 24, 2006
  4. anonym
    Replies:
    1
    Views:
    1,052
    Knute Johnson
    Jan 15, 2009
  5. Xah Lee
    Replies:
    0
    Views:
    360
    Xah Lee
    Jan 31, 2005
Loading...

Share This Page