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

Discussion in 'Javascript' started by vigi98, Feb 19, 2004.

  1. vigi98

    vigi98 Guest

    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.
     
    vigi98, Feb 19, 2004
    #1
    1. Advertising

  2. "vigi98" <> wrote in message
    news:...
    > 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.
     
    Richard Cornford, Feb 19, 2004
    #2
    1. Advertising

  3. vigi98

    vigi98 Guest

    > 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.
     
    vigi98, Feb 20, 2004
    #3
  4. vigi98

    optimistx Guest

    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'+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.
     
    optimistx, Feb 20, 2004
    #4
  5. vigi98

    Evertjan. Guest

    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)
     
    Evertjan., Feb 20, 2004
    #5
  6. JRS: In article <>, seen
    in news:comp.lang.javascript, vigi98 <> 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.
     
    Dr John Stockton, Feb 20, 2004
    #6
  7. (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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Feb 20, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. baumann@pan
    Replies:
    1
    Views:
    746
    Richard Bos
    Apr 15, 2005
  2. Song Ma
    Replies:
    2
    Views:
    235
    Charles Oliver Nutter
    Jul 20, 2008
  3. thunk
    Replies:
    1
    Views:
    315
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    489
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    627
    thunk
    Apr 3, 2010
Loading...

Share This Page