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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vigi98

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

    Hello all,

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

    Thanks a lot.
  • Richard Cornford

    #2
    Re: replace "&&quo t; by its URL symbol "%26&qu ot;

    "vigi98" <vigi98@free.fr > wrote in message
    news:c1df2002.0 402191107.dfbbd 70@posting.goog le.com...[color=blue]
    > Can someone confirme that this:
    > var strURLpiece = "UK & Ireland";
    > strURLpiece.rep lace("&", "%26");
    >replaces all occurrence of the character & by %26 in
    >strURLpiece, ie that strURLpiece becomes "UK %26 Ireland" ?[/color]

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

    Richard.


    Comment

    • vigi98

      #3
      Re: replace &quot;&amp;&quo t; by its URL symbol &quot;%26&qu ot;

      > If anyone does then don't trust them because it won't.[color=blue]
      >
      > Richard.[/color]

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

      Comment

      • optimistx

        #4
        Re: replace &quot;&amp;&quo t; by its URL symbol &quot;%26&qu ot;

        vigi98 wrote:[color=blue][color=green]
        >>If anyone does then don't trust them because it won't.
        >>
        >>Richard.[/color]
        >
        >
        > Thanks for this very helpfull solution. Nevertheless, what is the
        > solution ? I also tried this:
        > strURLpiece.rep lace(/&/,"%26");
        > but it does not work better.[/color]
        This worked in ie 6.0 and mozilla 1.6:

        alert("UK & Ireland".replac e(/[&]/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(str ing)=
        <input name="a" type="text" value="?"><br>
        <input type="button" value="evaluoi"
        onclick="docume nt.forms['fname'].a.value=evaluo i(document.form s['fname'].r.value,docume nt.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.

        Comment

        • Evertjan.

          #5
          Re: replace &quot;&amp;&quo t; by its URL symbol &quot;%26&qu ot;

          optimistx wrote on 20 feb 2004 in comp.lang.javas cript:
          [color=blue]
          > alert("UK & Ireland".replac e(/[&]/g,"%26"));
          >[/color]

          Without the [] works as well:

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

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: replace &quot;&amp;&quo t; by its URL symbol &quot;%26&qu ot;

            vigi98@free.fr (vigi98) writes:
            [color=blue]
            > Thanks for this very helpfull solution. Nevertheless, what is the
            > solution ? I also tried this:
            > strURLpiece.rep lace(/&/,"%26");
            > but it does not work better.[/color]

            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.rep lace(/&/,"%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.rep lace(/&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.rep lace(/\x26/,"%26");

            Good luck.
            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Dr John Stockton

              #7
              Re: replace &quot;&amp;&quo t; by its URL symbol &quot;%26&qu ot;

              JRS: In article <c1df2002.04021 91107.dfbbd70@p osting.google.c om>, seen
              in news:comp.lang. javascript, vigi98 <vigi98@free.fr > posted at Thu, 19
              Feb 2004 11:07:16 :-
              [color=blue]
              >Can someone confirme that this:
              > var strURLpiece = "UK & Ireland";
              > strURLpiece.rep lace("&", "%26");
              >replaces all occurrence of the character & by %26 in strURLpiece, ie
              >that strURLpiece becomes "UK %26 Ireland" ?[/color]

              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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              Working...