473,513 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String.replace and /

I am trying to replace the /'s in an URL with %2F
So if the URL was
http://www.netscape.com/sports/
I want it to be
http:%2F%2Fwww.netscape.com%2Fsports%2F

To do this I am trying the following JavaScript

str="http://www.netscape.com/sports/"
replaceStr="%2F"

regExp = /\//g

str2=str.replace(regExp, replaceStr)

and it doesn't work.
If I replace, say the http - it works fine - so there is something
wrong with the regular Expression.

I have also tried:

delim = "/g"
regExpValue = "\/"
regExp = eval ("/" + regExpValue + delim)

and that didn't work either.

I receive no errors - but nothing is replaced.

I did get the following to replace just the first /

str2=str.replace("\/", "%2F")

but would liek to replace all the /'s
Jul 20 '05 #1
1 2122

Ed Brandmark <eb********@aol.com> wrote in message
news:5f**************************@posting.google.c om...
I am trying to replace the /'s in an URL with %2F
So if the URL was
http://www.netscape.com/sports/
I want it to be
http:%2F%2Fwww.netscape.com%2Fsports%2F

To do this I am trying the following JavaScript

str="http://www.netscape.com/sports/"
replaceStr="%2F"

regExp = /\//g

str2=str.replace(regExp, replaceStr)

and it doesn't work.
If I replace, say the http - it works fine - so there is something
wrong with the regular Expression.

I have also tried:

delim = "/g"
regExpValue = "\/"
regExp = eval ("/" + regExpValue + delim)

and that didn't work either.

I receive no errors - but nothing is replaced.

I did get the following to replace just the first /

str2=str.replace("\/", "%2F")

but would liek to replace all the /'s


Hi Ed,
consider the following examples:

str="http://www.netscape.com/sports/"
replaceStr="%2F"

regExp = new RegExp( "\/", "g");
alert( regExp);
str1=str.replace(regExp, replaceStr)
alert( str + "\n" + str1);

str2=str.replace( /\//g, replaceStr);
alert( str + "\n" + str2);
Jul 20 '05 #2

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

Similar topics

4
62086
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...
13
10988
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
13
2333
by: dimitris67 | last post by:
How can I replace an occurence of p(a string) in an other string(s) with np(new string).. char* replace _pattern(char *s,char *p,char *np) PLEASE HELP ME!!!!!
7
21681
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
32
14762
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
12
9616
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
9
2142
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
9123
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
50147
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
3
3262
by: kronus | last post by:
I'm receiving an xml file that has a child called modified and it represents a date value in the form of a string -- Nov 14, 2008 -- and in my app, I have items associated with each object and I'm...
0
7259
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,...
0
7158
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...
0
7535
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
7523
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...
0
5683
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,...
0
3232
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.