RegExp / Replace Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Renfrew

    #1

    RegExp / Replace Question

    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


  • RobB

    #2
    Re: RegExp / Replace Question

    Ian Renfrew wrote:[color=blue]
    > To replace characters, I currently run the following code:
    >
    > oStr = oStr.replace(/\xFC/g,"\t"); // replace sub-value mark(s) with[/color]
    tab(s)[color=blue]
    >
    > Ideally, I'd like to replace the hard coded string \xFC with a[/color]
    variable such[color=blue]
    > as delim.
    >
    > I tried:
    >
    > oStr = oStr.replace(/delim/g,"\t"); // replace sub-value mark(s) with[/color]
    tab(s)[color=blue]
    >
    > but that does not appear to work.
    >
    > Is this possible? If so, how would I go about coding?
    >
    > Thanks and regards, Ian Renfrew[/color]

    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

    Comment

    • Mark Szlazak

      #3
      Re: RegExp / Replace Question

      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.

      Comment

      • John

        #4
        Re: RegExp / Replace Question

        "Ian Renfrew" <ian_renfrew@sy mpatico.ca> wrote in
        news:Fnw7e.5138 $MZ2.778074@new s20.bellglobal. com:
        [color=blue]
        > 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
        >
        >
        >[/color]
        either,

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

        or

        delim = /\xFC/g

        then

        oStr=oStr.repla ce(delim, "\t");

        Comment

        • Ian Renfrew

          #5
          Re: RegExp / Replace Question

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

          .... Ian

          "Ian Renfrew" <ian_renfrew@sy mpatico.ca> wrote in message
          news:Fnw7e.5138 $MZ2.778074@new s20.bellglobal. com...[color=blue]
          > 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
          >
          >[/color]


          Comment

          Working...