REGEXP problem

Discussion in 'JavaScript' started by cyber1, Dec 26, 2005.

  1. #1
    Hi-

    In my Perl scripts I have a lot of variables defined like so(not necessarily fonts, this is just an example):
    $ver_1b= qq|<font face="verdana,arial" size="1"><b>|;

    What I wanted was something where I would not need to have a substitution for each variable.
    I used to I use something like this:
    $line =~ s/%%ver_1b%%/$ver_1b/g;

    1 for each variable, as I knew very,very little REGEXP.

    Until I found this solution:
    $line =~ s/%%(\w+)%%/eval('$'."$1")/eg;
    This saved a lot of time and code.

    Now I have reached a point where I would like a similar solution in Javascript.
    Problem is the 'e' qualifier is not available in JS. And The eval function seems to work a little different.
    So heres what I'm using, 1 for each var.

    myText = myText.replace(/%%ver_2b%%/g,'<font face="verdana,arial" size="2"><b>');


    Until I find a solution I'm stuck with it.


    Thanks-
    -Bill
     
    cyber1, Dec 26, 2005 IP
  2. FeelLikeANut

    FeelLikeANut Peon

    Messages:
    330
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #2
    myText.replace(/%%(\w+)%%/g, window["$1"]);

    And this expression, "$line =~ s/%%(\w+)%%/eval('$'."$1")/eg;", should probably be written this way.

    $line =~ s/%%(\w+?)%%/$$1/eg;

    And additionally, while this may seem like less typing, it won't lead to an efficient or maintainable system. You really want to modularize as much as you can; allow yourself the option of completely altering one aspect of the system without affecting everything else. In this case, you need to separate what the program does from the Web interface. Right now it looks like you have the script, the HTML document, and the HTML document's appearence all clumped together; changing one would inherently require you to tinker with all three.
     
    FeelLikeANut, Dec 26, 2005 IP
  3. cyber1

    cyber1 Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    All points well taken.

    My question is this:
    myText.replace(/%%(\w+)%%/g, window["$1"]);

    Is that the equivalent of my Perl formula?
    $line =~ s/%%(\w+?)%%/$$1/eg;


    -Bill
     
    cyber1, Dec 27, 2005 IP