469,916 Members | 2,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

replace "&" by its URL symbol "%26"

Hello all,

Can someone confirme that this:
var strURLpiece = "UK & Ireland";
strURLpiece.replace("&", "%26");
replaces all occurrence of the character & by %26 in strURLpiece, ie
that strURLpiece becomes "UK %26 Ireland" ?

Thanks a lot.
Jul 20 '05 #1
6 50546
"vigi98" <vi****@free.fr> wrote in message
news:c1*************************@posting.google.co m...
Can someone confirme that this:
var strURLpiece = "UK & Ireland";
strURLpiece.replace("&", "%26");
replaces all occurrence of the character & by %26 in
strURLpiece, ie that strURLpiece becomes "UK %26 Ireland" ?


If anyone does then don't trust them because it won't.

Richard.
Jul 20 '05 #2
> If anyone does then don't trust them because it won't.

Richard.


Thanks for this very helpfull solution. Nevertheless, what is the
solution ? I also tried this:
strURLpiece.replace(/&/,"%26");
but it does not work better.
Jul 20 '05 #3
vigi98 wrote:
If anyone does then don't trust them because it won't.

Richard.

Thanks for this very helpfull solution. Nevertheless, what is the
solution ? I also tried this:
strURLpiece.replace(/&/,"%26");
but it does not work better.

This worked in ie 6.0 and mozilla 1.6:

alert("UK & Ireland".replace(/[&]/g,"%26"));

RegExp is for me so tricky to use that I made a small testbed to have
exercises in using it:

function evaluoi(r,s){
var re=new RegExp(r);
var myArray=new Array();
if (re){
myArray=re.exec(s);
if (myArray){alert(myArray.length+'\n'+myArray+'\n'+r e.source);}
return re.test(s) ;
}
return false;
}


<form name="fname" action="#" method="get">
RegExp=
<input name="r" type="text" value="(\d{5})([a-z]*)(\s)+(\*$)"><br>
string=
<input name="s" type="text" value="12345abc *"><br>
RegExp.test(string)=
<input name="a" type="text" value="?"><br>
<input type="button" value="evaluoi"
onclick="document.forms['fname'].a.value=evaluoi(document.forms['fname'].r.value,document.forms['fname'].s.value);">
</form>

You can ignore the initial example values in the form fields.

Note that typing expressions to a form is different from using the
expressions in the progrmam code.
Jul 20 '05 #4
optimistx wrote on 20 feb 2004 in comp.lang.javascript:
alert("UK & Ireland".replace(/[&]/g,"%26"));


Without the [] works as well:

alert("UK & Ireland &cetera".replace(/&/g,"%26"));

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #5
vi****@free.fr (vigi98) writes:
Thanks for this very helpfull solution. Nevertheless, what is the
solution ? I also tried this:
strURLpiece.replace(/&/,"%26");
but it does not work better.


You wanted the value of strURLpiece to change. For that you need an
assignment. The replace method does not change the string. So, at least;

strURLpiece = strURLpiece.replace(/&/,"%26");

The next problem is the "&". If the script is embedded in HTML, the
ampersand has a special meaning, and should be (HTML-)escaped:

strURLpiece = strURLpiece.replace(/&amp;/,"%26");

If the script is not embedded in Javascript, then you should not escape
the ampersand. If you want to avoid thinking about it, you can use a
Javascript escape:

strURLpiece = strURLpiece.replace(/\x26/,"%26");

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6
JRS: In article <c1*************************@posting.google.com> , seen
in news:comp.lang.javascript, vigi98 <vi****@free.fr> posted at Thu, 19
Feb 2004 11:07:16 :-
Can someone confirme that this:
var strURLpiece = "UK & Ireland";
strURLpiece.replace("&", "%26");
replaces all occurrence of the character & by %26 in strURLpiece, ie
that strURLpiece becomes "UK %26 Ireland" ?


It appears to replace all ampersands in "UK & Ireland".
It only replaces the first ampersand in "&&" & " & & "

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Peter Mount | last post: by
1 post views Thread by Asha | last post: by
2 posts views Thread by beachboy | last post: by
3 posts views Thread by Tanmaya Kulkarni | last post: by
7 posts views Thread by 一首诗 | last post: by
7 posts views Thread by John Nagle | last post: by
27 posts views Thread by sophia | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.