469,623 Members | 1,850 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

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

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

Mar 2 '08 #1
2 11449
On Mar 2, 12:51*pm, Xlect...@webtv.net (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
Mar 2 '08 #2
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

Mar 2 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by David | last post: by
5 posts views Thread by Ones Self | last post: by
4 posts views Thread by Er Galv?o Abbott | last post: by
1 post views Thread by Ed Brandmark | last post: by
2 posts views Thread by jatinder.singh | last post: by
21 posts views Thread by gary | last post: by
6 posts views Thread by kilik3000 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.