Need help with a simple problem

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

    Need help with a simple problem

    However, despite being simple, I can't figure it out (maybe because I'm a
    newbie).

    I'm trying to replace a string with another string, but it simply is not
    working (no error messages, just won't work).

    Basically, I have a document and I'd like to every occurrence of '&&1&&'
    with 'test'. Eventually the user will specify what '&&1&&' will be replaced
    with, but for now I can't get this simple test to work. What am I doing
    wrong?

    Thanks!

    <SCRIPT LANGUAGE="JavaS cript" type="text/javascript">
    <!--
    var sDocument = new String("This is a &&1&&.");

    alert (sDocument);

    sDocument.repla ce("&&1&&", "test");
    // sDocument.repla ce("&&1&&", txtPrompt.value );

    alert (sDocument);

    //-->
    </SCRIPT>


  • Lee

    #2
    Re: Need help with a simple problem

    SteveB said:[color=blue]
    >
    >However, despite being simple, I can't figure it out (maybe because I'm a
    >newbie).
    >
    >I'm trying to replace a string with another string, but it simply is not
    >working (no error messages, just won't work).[/color]

    the String.replace( ) method doesn't change the value of the string.
    It creates a new string, with the replacement made:

    var string1="alpha" ;
    var string2=string1 .replace("alph" ,"bet");

    Comment

    • SteveB

      #3
      Re: Need help with a simple problem

      Thanks Lee, worked like a charm!

      "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
      news:bnu79l01p8 j@drn.newsguy.c om...[color=blue]
      > SteveB said:[color=green]
      > >
      > >However, despite being simple, I can't figure it out (maybe because I'm a
      > >newbie).
      > >
      > >I'm trying to replace a string with another string, but it simply is not
      > >working (no error messages, just won't work).[/color]
      >
      > the String.replace( ) method doesn't change the value of the string.
      > It creates a new string, with the replacement made:
      >
      > var string1="alpha" ;
      > var string2=string1 .replace("alph" ,"bet");
      >[/color]


      Comment

      • SteveB

        #4
        Re: Need help with a simple problem

        One more question if I may. The replace function only seems to replace the
        first instance of the string it's looking for. What if there are multiple
        instances (for example, more than one &&1&& in my example), how can I
        replace them all (keep in mind that I don't know how many there might be)?

        Thanks!

        "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
        news:bnu79l01p8 j@drn.newsguy.c om...[color=blue]
        > SteveB said:[color=green]
        > >
        > >However, despite being simple, I can't figure it out (maybe because I'm a
        > >newbie).
        > >
        > >I'm trying to replace a string with another string, but it simply is not
        > >working (no error messages, just won't work).[/color]
        >
        > the String.replace( ) method doesn't change the value of the string.
        > It creates a new string, with the replacement made:
        >
        > var string1="alpha" ;
        > var string2=string1 .replace("alph" ,"bet");
        >[/color]


        Comment

        • Lee

          #5
          Re: Need help with a simple problem

          SteveB said:[color=blue]
          >
          >One more question if I may. The replace function only seems to replace the
          >first instance of the string it's looking for. What if there are multiple
          >instances (for example, more than one &&1&& in my example), how can I
          >replace them all (keep in mind that I don't know how many there might be)?[/color]

          The first argument to replace is more correctly a "regular expression",
          rather than a simple string. Regular expressions allow such things
          as wildcards and specifying that the replacement should be done
          "globally", throughout the string:

          string2=string1 .replace(/alph/g,"bet");

          The "g" indicates "global" replacement.

          Beware that some characters ("^[/", and others) have special
          meaning within regular expressions.

          Comment

          • SteveB

            #6
            Re: Need help with a simple problem

            Once again... thank you Lee! That did the trick!

            "Lee" <REM0VElbspamtr ap@cox.net> wrote in message
            news:bnudhm02dp a@drn.newsguy.c om...[color=blue]
            > SteveB said:[color=green]
            > >
            > >One more question if I may. The replace function only seems to replace[/color][/color]
            the[color=blue][color=green]
            > >first instance of the string it's looking for. What if there are multiple
            > >instances (for example, more than one &&1&& in my example), how can I
            > >replace them all (keep in mind that I don't know how many there might[/color][/color]
            be)?[color=blue]
            >
            > The first argument to replace is more correctly a "regular expression",
            > rather than a simple string. Regular expressions allow such things
            > as wildcards and specifying that the replacement should be done
            > "globally", throughout the string:
            >
            > string2=string1 .replace(/alph/g,"bet");
            >
            > The "g" indicates "global" replacement.
            >
            > Beware that some characters ("^[/", and others) have special
            > meaning within regular expressions.
            >[/color]


            Comment

            Working...