473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to make replace function replace globally in a string



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

Jul 3 '07 #1
5 9175
Lee
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...String:replace
--

Jul 3 '07 #2
d d
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
Jul 3 '07 #3
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","=").repla ce
("%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..._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","=").repla ce
("%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..._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.
--
Jul 3 '07 #4
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","=").repla ce
("%2 6","&");

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

~dd
Wow!

I wasn't aware of that.

It is the most handy.

Thanks. :-)
--
Jul 3 '07 #5
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.

Jul 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
12
by: Brian | last post by:
I want to use regxp to check that a form input contains at least 1 non-space charcter. I'd like to only run this if the browser supports it. For DOM stuff, I'd use if (documentGetElementById) {}...
6
by: marktm | last post by:
Hi- I am trying to use setInterval to call a method within an object and have not had any luck. I get "Object doesn't support this property or method" when I execute the following code. What I...
10
by: DataBard007 | last post by:
Hello Access Gurus: I use Win98SE and Access97. I just built a simple Access97 application which holds all contact information for my personal contacts, such as first name, last name, address,...
4
by: Office Drone | last post by:
I already figured out how to replace new & delete operators, which wasn't that hard and this is working great globally. However, aside from new & delete, memory allocation is also used via...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
3
by: Roy W. Andersen | last post by:
Hi, I need to do some replace-calls on certain strings in order to replace smiley glyphs and other keywords with graphical icons on the client. Unfortunately, my knowledge of regular expressions...
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.