RegExp / Replace Question

I

Ian Renfrew

To replace characters, I currently run the following code:

oStr = oStr.replace(/\xFC/g,"\t"); // replace sub-value mark(s) with tab(s)

Ideally, I'd like to replace the hard coded string \xFC with a variable such
as delim.

I tried:

oStr = oStr.replace(/delim/g,"\t"); // replace sub-value mark(s) with tab(s)

but that does not appear to work.

Is this possible? If so, how would I go about coding?

Thanks and regards, Ian Renfrew
 
R

RobB

Ian said:
To replace characters, I currently run the following code:

oStr = oStr.replace(/\xFC/g,"\t"); // replace sub-value mark(s) with tab(s)

Ideally, I'd like to replace the hard coded string \xFC with a variable such
as delim.

I tried:

oStr = oStr.replace(/delim/g,"\t"); // replace sub-value mark(s) with tab(s)

but that does not appear to work.

Is this possible? If so, how would I go about coding?

Thanks and regards, Ian Renfrew

Use the RegExp constructor to compile the regex at run-time:

var re = new RegExp(delim, 'g');
oStr = oStr.replace(re, '\t');

http://www.webreference.com/js/column5/define.html
 
M

Mark Szlazak

You'll need to use the regular expression constructor not regular
expression literals in your str.replace() function. The RegExp
constructor is passed a string that represents the regular expression
pattern, so you can make that string by concatenating some variable.
 
J

John

To replace characters, I currently run the following code:

oStr = oStr.replace(/\xFC/g,"\t"); // replace sub-value mark(s) with
tab(s)

Ideally, I'd like to replace the hard coded string \xFC with a
variable such as delim.

I tried:

oStr = oStr.replace(/delim/g,"\t"); // replace sub-value mark(s) with
tab(s)

but that does not appear to work.

Is this possible? If so, how would I go about coding?

Thanks and regards, Ian Renfrew
either,

delim=new RegExp("\\xFC", "g");

or

delim = /\xFC/g

then

oStr=oStr.replace(delim, "\t");
 
I

Ian Renfrew

Thats done the trick. Learn something new each day. Thanks Rob, Mark and
John for your assistance.

.... Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top