469,935 Members | 1,469 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

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 2036

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by higabe | last post: by
13 posts views Thread by dimitris67 | last post: by
7 posts views Thread by VMI | last post: by
32 posts views Thread by tshad | last post: by
9 posts views Thread by Crirus | last post: by
15 posts views Thread by morleyc | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.