replace+eval

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

    #1

    replace+eval

    Hi there!

    I'm a javascript begginner, and I wrote a small piece of code that uses
    the replace and the eval methods.

    When calling them separately, they seem to work and everything is ok.
    But when combining them, nothing happens.

    So, that's the code:

    alfaVALUE = 9;

    //step 1
    function f(x) {return eval(x+"VALUE") ;}
    f("alfa");

    //returns 9, OK for me



    //step 2
    myString = 'alfaVAR and betaVAR';
    myString.replac e(/(\w*)VAR/g, "$1VALUE");

    //replaces with "alfaVALUE and betaVALUE", OK for me


    //step 3, I'd like to use the values stored in alfaVALUE and betaVALUE
    result = myString.replac e(/(\w*)VAR/g, f($1));

    //???


    Thanks in advance

    Chris
  • Evertjan.

    #2
    Re: replace+eval

    Cristian Tarsoaga wrote on 18 apr 2005 in comp.lang.javas cript:

    [color=blue]
    > I'm a javascript begginner, and I wrote a small piece of code that uses
    > the replace and the eval methods.[/color]

    eval() is evil, never use it, You don't need it.
    See the archive of this NG.

    in a browser ude the window[] global identifier:

    function f(x) {return window[x+"VALUE"];}

    elswhere or everywhere use self[]

    function f(x) {return self[x+"VALUE"];}

    [color=blue]
    > When calling them separately, they seem to work and everything is ok.
    > But when combining them, nothing happens.
    >
    > So, that's the code:
    >
    > alfaVALUE = 9;
    >
    > //step 1
    > function f(x) {return eval(x+"VALUE") ;}
    > f("alfa");
    >
    > //returns 9, OK for me
    >
    >
    >
    > //step 2
    > myString = 'alfaVAR and betaVAR';
    > myString.replac e(/(\w*)VAR/g, "$1VALUE");
    >
    > //replaces with "alfaVALUE and betaVALUE", OK for me
    >
    >
    > //step 3, I'd like to use the values stored in alfaVALUE and betaVALUE
    > result = myString.replac e(/(\w*)VAR/g, f($1));[/color]

    You can .replace with a function,
    but a string argument of that function
    cannot be modified by the $1 output.

    [and you should have defined betaVALUE]



    --
    Evertjan.
    The Netherlands.
    (Replace all crosses with dots in my emailaddress)

    Comment

    • Cristian Tarsoaga

      #3
      Re: replace+eval

      Thanks Evertjan.

      Still, I cannot fully use this information, but maybe I'm missing something!
      I tried to write it as


      alfaVALUE = 9;
      betaVALUE = 17;
      myText = "alfaVAR and betaVAR and even more goes here";
      myText.replace(/(\w*)VAR/g, window[$1+"VALUE"]);



      and still, some error occurs.
      I also didn't understood what you meant by saying that
      [color=blue]
      >
      > You can .replace with a function,
      > but a string argument of that function
      > cannot be modified by the $1 output.
      >[/color]

      I will use the code inside a browser.

      Thanks for your help, maybe you still can give me some hint

      Chris

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: replace+eval

        "Evertjan." <exjxw.hannivoo rt@interxnl.net > writes:
        [color=blue]
        > eval() is evil, never use it, You don't need it.
        > See the archive of this NG.[/color]

        Hear, hear.
        [color=blue]
        > in a browser ude the window[] global identifier:
        >
        > function f(x) {return window[x+"VALUE"];}
        >
        > elswhere or everywhere use self[][/color]

        "self" is also a browser property. It refers to the current window,
        corresponding to the HTML target "_self".

        A safe way to get the global object is:
        var global = (function(){ret urn this;})();
        Then use that.
        ....[color=blue][color=green]
        >> myString.replac e(/(\w*)VAR/g, "$1VALUE");[/color][/color]
        ....[color=blue][color=green]
        >> result = myString.replac e(/(\w*)VAR/g, f($1));[/color][/color]

        Notice how the second argument is a string in the first case. The
        substring "$1" has a specific meaning inside such a pattern.
        In the second case, the "$1" is not inside a string, so it's used
        as a variable, an undefined one at that.

        In any case, even if "$1" meant something, the value of "f($1)" is
        calculated once, before calling "replace". That will not give you
        different results for the two matches.

        To make a computed replacement, you can use a function argument:

        result = myString.replac e(/(\w*)VAR/g,
        function(match, submatch,index) {
        return f(submatch);
        });

        /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

        • Evertjan.

          #5
          Re: replace+eval

          Cristian Tarsoaga wrote on 18 apr 2005 in comp.lang.javas cript:
          [color=blue]
          > Thanks Evertjan.
          >
          > Still, I cannot fully use this information, but maybe I'm missing
          > something! I tried to write it as
          >
          >
          > alfaVALUE = 9;
          > betaVALUE = 17;
          > myText = "alfaVAR and betaVAR and even more goes here";
          > myText.replace(/(\w*)VAR/g, window[$1+"VALUE"]);
          >
          >
          >
          > and still, some error occurs.
          > I also didn't understood what you meant by saying that
          >[/color]

          As I said, you simply cannot do that $1 is a part of a textlitteral,
          not a javascript variable in itself, and the textlitteral has to be the
          second parametere of .replace() itself.

          What you want is impossible this way!!!

          =============== =======

          Test:

          <script type='text/javascript'>

          alfaVALUE = 9;
          betaVALUE = 17;
          myText = "alfaVAR and betaVAR and even more goes here";

          r=myText.replac e(/(\w*)VAR/g, window["$1VALUE"]);

          document.write( r+'<br>');
          // undefined and undefined and even more goes here

          r=myText.replac e(/(\w*)VAR/g, "$1VALUE");

          document.write( r);
          // alfaVALUE and betaVALUE and even more goes here

          </script>

          =============== ========

          The best you can do is this:

          =============== ========

          <script type='text/javascript'>

          alfaV = 9;
          betaV = 17;
          myText = "alfaVAR and betaVAR and even more goes here";

          r=
          myText.replace(/alfaVAR/g,alfaV).replac e(/betaVAR/g,betaV);

          document.write( r);
          // 9 and 17 and even more goes here

          </script>



          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Douglas Crockford

            #6
            Re: replace+eval

            Cristian Tarsoaga wrote:[color=blue]
            > I'm a javascript begginner, and I wrote a small piece of code that uses
            > the replace and the eval methods.[/color]

            [color=blue]
            > function f(x) {return eval(x+"VALUE") ;}[/color]

            Who taught you to use eval that way? Do you have an incompetent teacher?
            Do you have a book from an incompetent author?

            The wicked must be punished!

            Comment

            Working...