473,732 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("%2 F","/").replace("%3F ","?").replace( "%3D","=").repl ace("%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,"?").replac e(/%3D/g,"=").replac e(
/%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 9294
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","=").repl ace("%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,"?").replac e(/%3D/g,"=").replac e(
/%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("%2 F","/").replace("%3F ","?").replace( "%3D","=").repl ace("%2
6","&");
Did you not consider str1=unescape(s tr); ?

~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("%2 F","/").replace("%3F ","?").replace( "%3D","=").repl ace
("%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,"?").replac e(/%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:Str ing:replace
Lee wrote:
V S Rawat said:

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

str1 =
str.replace("%2 F","/").replace("%3F ","?").replace( "%3D","=").repl ace
("%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,"?").replac e(/%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:Str ing:replace
The following did it. With global flag. To be safer, I added ignore
flag also.

str1 =
str.replace("%2 F","/","gi").replace ("%3F","?","gi" ).replace("%3D" ,"=","g
i").replace("%2 6","&","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("%2 F","/").replace("%3F ","?").replace( "%3D","=").repl ace
("%2 6","&");

Did you not consider str1=unescape(s tr); ?

~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","=").repl ace("%2
6","&");

Did you not consider str1=unescape(s tr); ?
It makes more sense to be recommending the ECMA 262 3rd edition
specified global - decodeURICompon ent - 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
62110
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 supposed to write this function? String.replace(/</g,'&lt;');
12
4038
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) {} Is there an object/feature detection I can use to check for regxp string manipulation support? --
6
15224
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 am doing wrong? Your help will be much appreciated. Thanks
10
5788
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, city, state, etc. When the user wants to search for a particular record, he does two things: 1. On the form is a text box on which he enters the text he is searching for.
4
2295
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 calling malloc/calloc & free functions. What is the proper way to override these functions globally, not from inside the class? When I simply replace it like:
6
4896
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 the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
10
9609
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 Replacement As String) As String '... End Function
3
3000
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 is somewhat limited to say the least, so I'm struggling with making it work as I wand. What I have is an associative array like this: smileys = 'smile.gif'; smileys = 'sad.gif';
4
3146
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 want to optimize it, so i make a static member instance instead of using static func of Regex; static Regex filter= new Regex(".*0\\.000000.*\n", RegexOptions.Compiled); And use the static instance to replace the string....
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.