Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > RegExp / Replace Question

Reply
Thread Tools

RegExp / Replace Question

 
 
Ian Renfrew
Guest
Posts: n/a
 
      04-14-2005
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


 
Reply With Quote
 
 
 
 
RobB
Guest
Posts: n/a
 
      04-14-2005
Ian Renfrew wrote:
> 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

 
Reply With Quote
 
 
 
 
Mark Szlazak
Guest
Posts: n/a
 
      04-14-2005
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.

 
Reply With Quote
 
John
Guest
Posts: n/a
 
      04-14-2005
"Ian Renfrew" <(E-Mail Removed)> wrote in
news:Fnw7e.5138$(E-Mail Removed):

> 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");

 
Reply With Quote
 
Ian Renfrew
Guest
Posts: n/a
 
      04-14-2005
Thats done the trick. Learn something new each day. Thanks Rob, Mark and
John for your assistance.

.... Ian

"Ian Renfrew" <(E-Mail Removed)> wrote in message
news:Fnw7e.5138$(E-Mail Removed).. .
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
new RegExp().test() or just RegExp().test() Matěj Cepl Javascript 3 11-24-2009 02:41 PM
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Ruby 1.9 - ArgumentError: incompatible encoding regexp match(US-ASCII regexp with ISO-2022-JP string) Mikel Lindsaar Ruby 0 03-31-2008 10:27 AM
Programmatically turning a Regexp into an anchored Regexp Greg Hurrell Ruby 4 02-14-2007 06:56 PM
RegExp.exec() returns null when there is a match - a JavaScript RegExp bug? Uldis Bojars Javascript 2 12-17-2006 09:59 PM



Advertisments