javascript replace function...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tascienu@ecoaches.com

    javascript replace function...

    consider this code below:

    var toremove = "some";
    var tolook ="I am looking for some other text in this string toremove";

    if i do this:

    tolook = tolook.replace(/toremove/gi,'');

    will to look replace the text inside the toremove variable, or will it
    just replace the variable name?

    thanks for your help...

  • Rob B

    #2
    Re: javascript replace function...



    You're using a regular expression literal - so the string "toremove" is,
    erm, *literally* the pattern in question. If you want the regular
    expression evaluated (as a JavaScript expression) at runtime, you'll
    need to use the constructor:

    var toremove = "some";
    var re = new RegExp(toremove , 'gi');
    var tolook ="I am looking for some other text in this string toremove";

    tolook = tolook.replace( re, '');




    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!

    Comment

    • Mick White

      #3
      Re: javascript replace function...

      tascienu@ecoach es.com wrote:
      [color=blue]
      > var toremove = "some";
      > var tolook ="I am looking for some other text in this string toremove";
      >
      >
      > tolook = tolook.replace(/toremove/gi,'');
      >
      > will to look replace the text inside the toremove variable, or will it
      > just replace the variable name?
      >[/color]

      You *could* use eval():
      tolook = tolook.replace( eval("/"+toremove+ "/gi"),'');
      An appropriate use, I would say.
      Mick

      Comment

      • Michael Winter

        #4
        Re: javascript replace function...

        On Tue, 07 Dec 2004 16:49:29 GMT, Mick White
        <mwhite13BOGUS@ rochester.rr.co m> wrote:

        [snip]
        [color=blue]
        > tolook = tolook.replace( eval("/"+toremove+ "/gi"),'');
        > An appropriate use, I would say.[/color]

        I disagree. The RegExp constructor, which Rob mentioned, is the better
        solution.

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Mick White

          #5
          Re: javascript replace function...

          Michael Winter wrote:
          [color=blue]
          > On Tue, 07 Dec 2004 16:49:29 GMT, Mick White
          > <mwhite13BOGUS@ rochester.rr.co m> wrote:
          >
          > [snip]
          >[color=green]
          >> tolook = tolook.replace( eval("/"+toremove+ "/gi"),'');
          >> An appropriate use, I would say.[/color]
          >
          >
          > I disagree. The RegExp constructor, which Rob mentioned, is the better
          > solution.[/color]

          The RegExp constructor has it drawbacks, I'm referring to regexes
          compiled on the fly and those pesky escape characters.
          Mick

          Comment

          Working...