473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 11659
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David | last post by:
Hi, I've had a search through google but couldn't really find the answer I was looking for.I'm new to PHP, so please take it <relatively> easy. I've created a script which runs some SNMP...
5
by: Ones Self | last post by:
Hi all: I'm trying to replace using a regexp read from a file: $string = '123 456 789'; # these two are usualy read from a file, # and so have to be in variables. $re = '()'; $rep = '|$1|';
4
by: Er Galv?o Abbott | last post by:
Greetings. I have a function that does some pattern matching with JS's RegEx and I'm trying to use a variable inside of it. Nothing that I've done worked, so please help me. Here is the func:...
1
by: Ed Brandmark | last post by:
I am trying to replace the /'s in an URL with %2F So if the URL was http://www.netscape.com/sports/ I want it to be http:%2F%2Fwww.netscape.com%2Fsports%2F To do this I am trying the following...
2
by: jatinder.singh | last post by:
Hi All, I am trying to Format a given TSQL code so that it is well indented and easy to understand It is moderately working with simple scripts, but where the script involve some part of...
6
by: Jake Barnes | last post by:
This function has always worked for me just fine: function nl2br_js(myString){ // 02-18-06 - this function imitates the PHP command nl2br, which finds newlines in a string // and replaces them...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
5
by: SM | last post by:
Hello, I have an <ul>, and when i click on a item i want to add a class to that item. The class itself will change some display properties, using CSS. See code below. But, whenever i click on a...
6
by: kilik3000 | last post by:
How do you do a global replace in a string without using a regex? For example, "ABBA".replace("B", "") returns -"ABA" // I'd like this to be a global replace "ABBA".replace(/B/g, "") ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.