By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

String.replace and /

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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.