473,417 Members | 1,498 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

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 55928
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Stephan Koser | last post by:
Hi, on my website i have some $rarr; tags - that shows usually a right arrow. Now I have Windows XP Professional and IE 6.0.x and it shows a square with a kind of a star. How can I get a right...
14
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have...
1
by: Asha | last post by:
hello how does javascript handle char like &? i have a dynamically build link and javascript cant handle values like &? why is taht?
2
by: beachboy | last post by:
I have a problem if the parameter has a "&" symbol in querystring, i think asp.net will split the value into 2 values as "&" is a spliter any advise can give me to solve this problem? P.S. I...
3
by: Tanmaya Kulkarni | last post by:
Hi, I need to pass a string having ampersand "&" in the query string like Company=Johnson&Johnson. How do I send it and receive it? TIA, Tanmaya
7
by: 一首诗 | last post by:
Is there any simple way to solve this problem?
7
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
by: tavares | last post by:
--------------------------------------------------------------------------------------------------------------------------------------------- (Apologies for cross-posting) Symposium...
27
by: sophia | last post by:
Dear all, why in the following program #include<stdio.h> #include<stdlib.h> int main(void) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.