Using a variable name for the string.replace() regexp

Discussion in 'Javascript' started by X l e c t r i c, Mar 2, 2008.

  1. Here:

    http://bigbangfodder.fileave.com/res/sandr.html

    I'm trying to use string.replace() for a basic search and replace form
    using textarea values as the regexp and replacement values for
    string.replace().

    When I tried to use the textarea variable name for regexp it didn't work
    as I thought it would. For example:

    string.replace(/variablename/, replacementvariablename);

    This actually searched for variablename instead of the value it
    represents. Which actually makes sense. What I did to solve the problem
    was this:

    var e_v = eval('/' + variablename + '/' + g_b + c_s);

    where g_b and c_s are variable names for global and sensitivity
    depending upon whether or not checkboxes are checked.

    Then I follow that with:

    t_ai.value = t_ai.value.replace(e_v, t_ar);

    t_ai is the variable name for the textarea that holds the text that's
    being searched and t_ar is the variable name for the replacement
    textarea value.

    Is there a way that I can accomplish this without using eval() ?

    The text in the textareas and the "Clear Values" button are there as a
    convenience for anyone who takes a look to help me, and won't be there
    when I'm finished.

    Art
     
    X l e c t r i c, Mar 2, 2008
    #1
    1. Advertising

  2. X l e c t r i c

    RobG Guest

    On Mar 2, 12:51 pm, (X l e c t r i c) wrote:
    > Here:
    >
    > http://bigbangfodder.fileave.com/res/sandr.html
    >
    > I'm trying to use string.replace() for a basic search and replace form
    > using textarea values as the regexp and replacement values for
    > string.replace().
    >
    > When I tried to use the textarea variable name for regexp it didn't work
    > as I thought it would. For example:
    >
    > string.replace(/variablename/, replacementvariablename);
    >
    > This actually searched for variablename instead of the value it
    > represents. Which actually makes sense. What I did to solve the problem
    > was this:
    >
    > var e_v = eval('/' + variablename + '/' + g_b + c_s);


    Use RegExp as a constructor:

    var e_v = new RegExp(variablename, g_b + c_s);


    --
    Rob
     
    RobG, Mar 2, 2008
    #2
    1. Advertising

  3. RobG wrote:

    Use RegExp as a constructor:

    var e_v = new RegExp(variablename, g_b + c_s);

    That's outstanding Rob.

    I had tried different versions of the RegExp constructor but they
    included the slashes ( / ) and various combinations of quotes, and they
    obviously didn't work.

    Thank you very much.

    Art
     
    X l e c t r i c, Mar 2, 2008
    #3
    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. Ones Self

    RegExp Replace Using a Variable

    Ones Self, Feb 24, 2004, in forum: Perl
    Replies:
    5
    Views:
    582
    Gunnar Hjalmarsson
    Feb 26, 2004
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,401
    Joe Smith
    Apr 22, 2007
  3. Sébastien Maurette
    Replies:
    3
    Views:
    142
    David Vallner
    Oct 2, 2006
  4. Joao Silva
    Replies:
    16
    Views:
    364
    7stud --
    Aug 21, 2009
  5. Ones Self

    RegExp Replace Using a Variable

    Ones Self, Feb 25, 2004, in forum: Perl Misc
    Replies:
    6
    Views:
    92
    David K. Wall
    Feb 27, 2004
Loading...

Share This Page