Search and Replace for "$" in text

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

    #1

    Search and Replace for "$" in text

    I want to search a textbox and replace any instances of the dollar sign
    character. But when I do that it thinks it won't work because it think's I'm
    looking to replace something at the end of the line. How do I tell it to
    look for literal dollar signs?


  • Erwin Moller

    #2
    Re: Search and Replace for "$&quot ; in text

    Terry Olson wrote:
    [color=blue]
    > I want to search a textbox and replace any instances of the dollar sign
    > character. But when I do that it thinks it won't work because it think's
    > I'm looking to replace something at the end of the line.[/color]

    What is 'it'?
    Some mysterycode we can only guess about?
    [color=blue]
    >How do I tell it
    > to look for literal dollar signs?[/color]

    2 things that pop into mind:
    1) use String.substrin g() in combination with indexOf() and some basic
    coding.
    2) Or a regular expression which is probably more complex to understand for
    starters.
    I would advise you to do it with the first option.

    Good luck.

    Erwin Moller

    Comment

    • Terry Olson

      #3
      Re: Search and Replace for "$&quot ; in text

      I am using Regular Expressions, I tried putting just a simple \ in front of
      it; as in /\$/g and that seems to work, anyone got a better idea?

      "Erwin Moller"
      <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote in
      message news:413d6fe4$0 $34762$e4fe514c @news.xs4all.nl ...[color=blue]
      > Terry Olson wrote:
      >[color=green]
      > > I want to search a textbox and replace any instances of the dollar sign
      > > character. But when I do that it thinks it won't work because it think's
      > > I'm looking to replace something at the end of the line.[/color]
      >
      > What is 'it'?
      > Some mysterycode we can only guess about?
      >[color=green]
      > >How do I tell it
      > > to look for literal dollar signs?[/color]
      >
      > 2 things that pop into mind:
      > 1) use String.substrin g() in combination with indexOf() and some basic
      > coding.
      > 2) Or a regular expression which is probably more complex to understand[/color]
      for[color=blue]
      > starters.
      > I would advise you to do it with the first option.
      >
      > Good luck.
      >
      > Erwin Moller
      >[/color]


      Comment

      • Michael Winter

        #4
        Re: Search and Replace for &quot;$&quot ; in text

        On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com> wrote:
        [color=blue]
        > I am using Regular Expressions, I tried putting just a simple \ in front
        > of it; as in /\$/g and that seems to work, anyone got a better idea?[/color]

        That *is* how to do it. The only way, in fact. :)

        Mike


        Please don't top-post.

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

        Comment

        • Erwin Moller

          #5
          Re: Search and Replace for &quot;$&quot ; in text

          Michael Winter wrote:
          [color=blue]
          > On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com>
          > wrote:
          >[color=green]
          >> I am using Regular Expressions, I tried putting just a simple \ in front
          >> of it; as in /\$/g and that seems to work, anyone got a better idea?[/color]
          >
          > That *is* how to do it. The only way, in fact. :)
          >
          > Mike
          >
          >
          > Please don't top-post.
          >[/color]

          This works fine for me:

          $str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
          $str2 = ereg_replace ( '\$', '**', $str1);

          echo "str1= $str1<br>";
          echo "str2= $str2<br>";

          Comment

          • Erwin Moller

            #6
            Re: Search and Replace for &quot;$&quot ; in text

            Erwin Moller wrote:
            [color=blue]
            > Michael Winter wrote:
            >[color=green]
            >> On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com>
            >> wrote:
            >>[color=darkred]
            >>> I am using Regular Expressions, I tried putting just a simple \ in front
            >>> of it; as in /\$/g and that seems to work, anyone got a better idea?[/color]
            >>
            >> That *is* how to do it. The only way, in fact. :)
            >>
            >> Mike
            >>
            >>
            >> Please don't top-post.
            >>[/color]
            >
            > This works fine for me:
            >
            > $str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
            > $str2 = ereg_replace ( '\$', '**', $str1);
            >
            > echo "str1= $str1<br>";
            > echo "str2= $str2<br>";[/color]

            Oops, sorry, that was PHP of course. :-)
            Excuse me.

            Comment

            • Michael Winter

              #7
              Re: Search and Replace for &quot;$&quot ; in text

              On Tue, 07 Sep 2004 12:30:30 +0200, Erwin Moller
              <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> wrote:
              [color=blue]
              > Michael Winter wrote:
              >[color=green]
              >> On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com>
              >> wrote:
              >>[color=darkred]
              >>> I am using Regular Expressions, I tried putting just a simple \ in
              >>> front of it; as in /\$/g and that seems to work, anyone got a better
              >>> idea?[/color]
              >>
              >> That *is* how to do it. The only way, in fact. :)[/color][/color]

              [snip]
              [color=blue]
              > $str1 = '$Hello! All the dollars $ should $ be replaced by $ **';
              > $str2 = ereg_replace ( '\$', '**', $str1);[/color]

              If converted to Javascript, you don't need the backslash with the string
              argument form of the replace method. In that instance, it's a simple
              string comparison. If you are using a regular expression, then the
              backslash is necessary to ensure the ampersand isn't interpreted as a
              special character. This latter case is what I was referring to. So
              initially, your two options are:

              var str = '$Hello! All the dollars $ should $ be replaced by $ **',
              res;

              res = str.replace('$' , '**');

              or

              res = str.replace(/\$/, '**');

              However, both of these should only replace the first instance of an
              ampersand. If all occurances need to be replaced, you have to use regular
              expression the OP presented:

              res = str.replace(/\$/g, '**');

              Mike

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

              Comment

              • Evertjan.

                #8
                Re: Search and Replace for &quot;$&quot ; in text

                Michael Winter wrote on 07 sep 2004 in comp.lang.javas cript:
                [color=blue]
                > res = str.replace(/\$/, '**');
                >
                > However, both of these should only replace the first instance of an
                > ampersand. If all occurances need to be replaced, you have to use
                > regular expression the OP presented:
                >[/color]

                I know the $ is devaluating, but has it become an & ?

                ;-}

                --
                Evertjan.
                The Netherlands.
                (Please change the x'es to dots in my emailaddress,
                but let us keep the discussions in the newsgroup)

                Comment

                • Michael Winter

                  #9
                  Re: Search and Replace for &quot;$&quot ; in text

                  On 07 Sep 2004 13:10:09 GMT, Evertjan. <exjxw.hannivoo rt@interxnl.net >
                  wrote:
                  [color=blue]
                  > Michael Winter wrote on 07 sep 2004 in comp.lang.javas cript:
                  >[color=green]
                  >> res = str.replace(/\$/, '**');
                  >>
                  >> However, both of these should only replace the first instance of an
                  >> ampersand. If all occurances need to be replaced, you have to use
                  >> regular expression the OP presented:[/color]
                  >
                  > I know the $ is devaluating, but has it become an & ?
                  >
                  > ;-}[/color]

                  :P

                  Hmm. Yes, I meant a dollar symbol rather than an ampersand.

                  Mike

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

                  Comment

                  • Dr John Stockton

                    #10
                    Re: Search and Replace for &quot;$&quot ; in text

                    JRS: In article <opsdx7e9d6x13k vk@atlantis>, dated Tue, 7 Sep 2004
                    08:33:02, seen in news:comp.lang. javascript, Michael Winter <M.Winter@bl
                    ueyonder.co.inv alid> posted :[color=blue]
                    >On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com> wrote:
                    >[color=green]
                    >> I am using Regular Expressions, I tried putting just a simple \ in front
                    >> of it; as in /\$/g and that seems to work, anyone got a better idea?[/color]
                    >
                    >That *is* how to do it. The only way, in fact. :)[/color]

                    x = "34$56".replace (/\x24/, '#')
                    x = "34$56".replace (/\044/, '#')
                    x = "34$56".replace (/\u0024/, '#')
                    x = "34$56".replace (/[^\000-#%-\377]/, '#')

                    all give me "34#56".

                    By "only", you must mean "proper" or "best" rather than "unique".

                    --
                    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                    Web <URL:http://www.merlyn.demo n.co.uk/> - FAQqish topics, acronyms & links;
                    some Astro stuff via astro.htm, gravity0.htm; quotes.htm; pascal.htm; &c, &c.
                    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

                    Comment

                    • Michael Winter

                      #11
                      Re: Search and Replace for &quot;$&quot ; in text

                      On Tue, 7 Sep 2004 20:42:42 +0100, Dr John Stockton
                      <spam@merlyn.de mon.co.uk> wrote:
                      [color=blue]
                      > JRS: In article <opsdx7e9d6x13k vk@atlantis>, dated Tue, 7 Sep 2004
                      > 08:33:02, seen in news:comp.lang. javascript, Michael Winter
                      > <M.Winter@bluey onder.co.invali d> posted :[color=green]
                      >> On Tue, 07 Sep 2004 08:27:11 GMT, Terry Olson <twjolson@hotma il.com>
                      >> wrote:
                      >>[color=darkred]
                      >>> I am using Regular Expressions, I tried putting just a simple \ in
                      >>> front of it; as in /\$/g and that seems to work, anyone got a better
                      >>> idea?[/color]
                      >>
                      >> That *is* how to do it. The only way, in fact. :)[/color]
                      >
                      > x = "34$56".replace (/\x24/, '#')
                      > x = "34$56".replace (/\044/, '#')
                      > x = "34$56".replace (/\u0024/, '#')
                      > x = "34$56".replace (/[^\000-#%-\377]/, '#')
                      >
                      > all give me "34#56".
                      >
                      > By "only", you must mean "proper" or "best" rather than "unique".[/color]

                      The OP wanted to know if the backslash was necessary. I took that to mean,
                      "Is escaping necessary?". My reply was yes.

                      I must admit, I hadn't considered the last approach, but I think that
                      one's just a little over-the-top. :)

                      Mike

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

                      Comment

                      Working...