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

V

vigi98

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.
 
R

Richard Cornford

vigi98 said:
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.
 
V

vigi98

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

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

optimistx

vigi98 said:
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'+re.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.
 
E

Evertjan.

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"));
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
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 "&&" & " & & "
 
L

Lasse Reichstein Nielsen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top