Re: how to make replace function replace globally in a string



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.
--
.



Relevant Pages

  • Re: What is wrong with: puts "Data={" ?
    ... matched braces in the strings are fine: ... proc foo {flag} { ... If they don't match, you have to backslash them, without backslash it fails: ...
    (comp.lang.tcl)
  • Re: sloppy-char flag strangeness
    ... does not get performed when the sloppy-char flag is used. ... fail when this flag is used. ... operator is defined only for strings ... ...
    (comp.lang.fortran)
  • Re: search and select from a textfile
    ... >> write all the strings you read into a tstringlist when this flag is ... >> When you get to an end, check you matched boolean flag to see if there ... >Hi Dodgy, ... > readln(oldfile, oldfileline); ...
    (comp.lang.pascal.delphi.misc)
  • Re: perl efficiency -- fastest grepping?
    ... > Is there a more efficient way to grep for the strings to set some flag? ... > This works pretty well but this is only 4 strings. ... can use 'grep' to find what you need. ... sub do_resetpf { ...
    (comp.lang.perl.misc)
  • Re: Java vs. Pascal
    ... Strings zu tun. ... 500 pattern.split~Regex ... Mit wiederverwendetem pattern hat man da schon 30% der Zeit gespart. ... Das Potential der verschiedenen Methoden (RegEx, StringTokenizer, split2) ...
    (de.comp.lang.java)

Loading