RegExp / Replace Question

Discussion in 'Javascript' started by Ian Renfrew, Apr 14, 2005.

  1. Ian Renfrew

    Ian Renfrew Guest

    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
    Ian Renfrew, Apr 14, 2005
    #1
    1. Advertising

  2. Ian Renfrew

    RobB Guest

    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
    RobB, Apr 14, 2005
    #2
    1. Advertising

  3. Ian Renfrew

    Mark Szlazak Guest

    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.
    Mark Szlazak, Apr 14, 2005
    #3
  4. Ian Renfrew

    John Guest

    "Ian Renfrew" <> wrote in
    news:Fnw7e.5138$:

    > 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");
    John, Apr 14, 2005
    #4
  5. Ian Renfrew

    Ian Renfrew Guest

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

    .... Ian

    "Ian Renfrew" <> wrote in message
    news:Fnw7e.5138$...
    > 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
    >
    >
    Ian Renfrew, Apr 14, 2005
    #5
    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. Greg Hurrell
    Replies:
    4
    Views:
    152
    James Edward Gray II
    Feb 14, 2007
  2. Mikel Lindsaar
    Replies:
    0
    Views:
    467
    Mikel Lindsaar
    Mar 31, 2008
  3. Joao Silva
    Replies:
    16
    Views:
    344
    7stud --
    Aug 21, 2009
  4. Uldis  Bojars
    Replies:
    2
    Views:
    186
    Janwillem Borleffs
    Dec 17, 2006
  5. Matìj Cepl

    new RegExp().test() or just RegExp().test()

    Matìj Cepl, Nov 24, 2009, in forum: Javascript
    Replies:
    3
    Views:
    171
    Matěj Cepl
    Nov 24, 2009
Loading...

Share This Page