how to make replace function replace globally in a string

Discussion in 'Javascript' started by V S Rawat, Jul 3, 2007.

  1. V S Rawat

    V S Rawat Guest

    I was trying to use back-to-back replace functions to convert a url:

    str1 =
    str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
    6","&");

    It didn't replace all 4 types of strings.

    Then, I googled and found this suggestion of some JavaScript Tutorials,
    so I used replace with a regex with a global switch:

    str1 =
    str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").replace(
    /%26/g,"&");

    and it did replace all the occurances of all the strings.

    OK. my problem is solved, but I am curious why should the first method
    not work?

    Thanks.
    --
    V S Rawat, Jul 3, 2007
    #1
    1. Advertising

  2. V S Rawat

    Lee Guest

    V S Rawat said:
    >
    >
    >
    >I was trying to use back-to-back replace functions to convert a url:
    >
    >str1 =
    >str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
    >6","&");
    >
    >It didn't replace all 4 types of strings.
    >
    >Then, I googled and found this suggestion of some JavaScript Tutorials,
    >so I used replace with a regex with a global switch:
    >
    >str1 =
    >str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").replace(
    >/%26/g,"&");
    >
    >and it did replace all the occurances of all the strings.
    >
    >OK. my problem is solved, but I am curious why should the first method
    >not work?


    Because there's no "global" flag specified.
    Replacing only the first occurrence has been the default behavior
    of replacement functions since the beginning of time.
    If you're using a string as the first parameter, instead of a RegEx,
    you can add an optional third parameter to specify flags:

    http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:String:replace


    --
    Lee, Jul 3, 2007
    #2
    1. Advertising

  3. V S Rawat

    d d Guest

    V S Rawat wrote:
    > I was trying to use back-to-back replace functions to convert a url:
    > str1 =
    > str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
    > 6","&");


    Did you not consider str1=unescape(str); ?

    ~dd
    d d, Jul 3, 2007
    #3
  4. V S Rawat

    V S Rawat Guest

    Lee wrote:

    > V S Rawat said:
    > >
    > >
    > >
    > > I was trying to use back-to-back replace functions to convert a
    > > url:
    > >
    > > str1 =
    > > str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace
    > > ("%2 6","&");
    > >
    > > It didn't replace all 4 types of strings.
    > >
    > > Then, I googled and found this suggestion of some JavaScript
    > > Tutorials, so I used replace with a regex with a global switch:
    > >
    > > str1 =
    > > str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").repl
    > > ace( /%26/g,"&");
    > >
    > > and it did replace all the occurances of all the strings.
    > >
    > > OK. my problem is solved, but I am curious why should the first
    > > method not work?

    >
    > Because there's no "global" flag specified.
    > Replacing only the first occurrence has been the default behavior
    > of replacement functions since the beginning of time.
    > If you're using a string as the first parameter, instead of a RegEx,
    > you can add an optional third parameter to specify flags:
    >
    > http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Glo
    > bal_Objects:String:replace


    Lee wrote:

    > V S Rawat said:
    > >
    > >
    > >
    > > I was trying to use back-to-back replace functions to convert a
    > > url:
    > >
    > > str1 =
    > > str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace
    > > ("%2 6","&");
    > >
    > > It didn't replace all 4 types of strings.
    > >
    > > Then, I googled and found this suggestion of some JavaScript
    > > Tutorials, so I used replace with a regex with a global switch:
    > >
    > > str1 =
    > > str.replace(/%2F/g,"/").replace(/%3F/g,"?").replace(/%3D/g,"=").repl
    > > ace( /%26/g,"&");
    > >
    > > and it did replace all the occurances of all the strings.
    > >
    > > OK. my problem is solved, but I am curious why should the first
    > > method not work?

    >
    > Because there's no "global" flag specified.
    > Replacing only the first occurrence has been the default behavior
    > of replacement functions since the beginning of time.
    > If you're using a string as the first parameter, instead of a RegEx,
    > you can add an optional third parameter to specify flags:
    >
    > http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Glo
    > bal_Objects:String:replace


    The following did it. With global flag. To be safer, I added ignore
    flag also.

    str1 =
    str.replace("%2F","/","gi").replace("%3F","?","gi").replace("%3D","=","g
    i").replace("%26","&","gi"))

    Thanks.

    Actually, I am learning from free ebooks downloaded from net, and most
    of them don't bother to give the full syntax. They just give one or two
    simple usage of each keyword.

    The ebook I am having didn't even tell about using regex in replace.
    That I had found on google.

    Thanks for pointing me out to mozilla resources.
    --
    V S Rawat, Jul 3, 2007
    #4
  5. V S Rawat

    V S Rawat Guest

    d d wrote:

    > V S Rawat wrote:
    > > I was trying to use back-to-back replace functions to convert a
    > > url: str1 =
    > > str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace
    > > ("%2 6","&");

    >
    > Did you not consider str1=unescape(str); ?
    >
    > ~dd


    Wow!

    I wasn't aware of that.

    It is the most handy.

    Thanks. :)
    --
    V S Rawat, Jul 3, 2007
    #5
  6. d d wrote:
    >V S Rawat wrote:
    >> I was trying to use back-to-back replace functions to convert a url:
    >> str1 =
    >> str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2
    >> 6","&");

    >
    > Did you not consider str1=unescape(str); ?


    It makes more sense to be recommending the ECMA 262 3rd edition
    specified global - decodeURIComponent - function for reversing this
    style of encoding, as it is part of the language, rather than the
    non-standardised - unescape - function, which only may be provided as
    part of a browser object model.

    Richard.
    Richard Cornford, Jul 3, 2007
    #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. Steve Harrad
    Replies:
    5
    Views:
    4,471
    Mike Treseler
    Dec 22, 2003
  2. valentin tihomirov

    Actual is not a globally static expression

    valentin tihomirov, Feb 10, 2004, in forum: VHDL
    Replies:
    7
    Views:
    8,664
    Jonathan Bromley
    Feb 17, 2004
  3. Leo R

    Try catch (globally)

    Leo R, Nov 3, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    332
    Leo R
    Nov 4, 2003
  4. Dave W.
    Replies:
    7
    Views:
    2,804
    J. Cliff Dyer
    Apr 16, 2010
  5. giri alamuri
    Replies:
    5
    Views:
    132
    giri alamuri
    Sep 12, 2003
Loading...

Share This Page