help with string replace - for doing selective replace

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

    help with string replace - for doing selective replace

    Hello

    I wish to replace all the characters in a string except those which
    are inside '<' & '>' characters. And there could be multiple
    occurences of < & > within the string.

    e.g. string = "this is an example of <how> many words could be hidden
    <under> these characters"

    now, from this string all the characters should be searched & replaced
    by a certain logic except <how> & <under>

    I am accepting the string from a textarea form field and there can be
    no or multiple occurences of words within < >

    Any help will be highly appreciated.
  • Evertjan.

    #2
    Re: help with string replace - for doing selective replace

    Prasad S wrote on 20 aug 2004 in comp.lang.javas cript:
    [color=blue]
    > I wish to replace all the characters in a string except those which
    > are inside '<' & '>' characters. And there could be multiple
    > occurences of < & > within the string.
    >
    > e.g. string = "this is an example of <how> many words could be hidden
    > <under> these characters"
    >[/color]

    s = "an example of <how> words <under> these characters"

    s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • Evertjan.

      #3
      Re: help with string replace - for doing selective replace

      Prasad S wrote on 26 aug 2004 in comp.lang.javas cript:[color=blue]
      > "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message
      > news:<Xns954B9C D76B016eejj99@1 94.109.133.29>. ..[color=green]
      >>
      >> s = "an example of <how> words <under> these characters"
      >> s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')[/color]
      >
      > Thanks for a very prompt response! I tried your suggestion, its
      > retruning the following:
      >
      > s = "an example of <how> words <under> these characters"
      > s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')
      > result: <how>, <under>
      >
      > However, i wish to have the following result:
      > e.g. i want to replace all 'a' s with 'Z' in the string which is not
      > within < & >
      > s = "an example of <how many> words <as good as> these characters"
      > RESULT should be: "Zn exZmple of <how many> words <as good as> these
      > chZrZcters"
      > i.e. it should replace all the a's which are not inside the < & > with
      > Zs[/color]

      <script type="text/javascript">

      Sin="an example of <how many> words <as good as> these characters"

      Sout = ""
      outside = true
      for (i=0;i<Sin.leng th;i++) {
      x = Sin.substr(i,1)
      if (x=='<')outside = false
      if (x=='>')outside = true
      if (x=='a'&&outsid e) x = 'Z'
      Sout+= x
      }
      alert(Sout)

      </script>


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

      Comment

      • bruce

        #4
        Re: help with string replace - for doing selective replace

        prasad_shir@red iffmail.com (Prasad S) wrote in message news:<beb94c5c. 0408260700.31af 3400@posting.go ogle.com>...[color=blue]
        > Hi Evertjan
        >
        > Thanks for a very prompt response! I tried your suggestion, its
        > retruning the following:
        >
        >
        > s = "an example of <how> words <under> these characters"
        >
        > s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')
        >
        > result: <how>, <under>
        >
        > However, i wish to have the following result:
        >
        > e.g. i want to replace all 'a' s with 'Z' in the string which is not
        > within < & >
        >
        > s = "an example of <how many> words <as good as> these characters"
        >
        > RESULT should be: "Zn exZmple of <how many> words <as good as> these
        > chZrZcters"
        >
        > i.e. it should replace all the a's which are not inside the < & > with
        > Zs
        >
        > I have tried several different ways, still not able to crack this....
        >
        > Any help will be useful....
        >
        > "Evertjan." <exjxw.hannivoo rt@interxnl.net > wrote in message news:<Xns954B9C D76B016eejj99@1 94.109.133.29>. ..[color=green]
        > >
        > > s = "an example of <how> words <under> these characters"
        > >
        > > s = s.replace(/[^<]*(<[^>]*>)[^<]*/g,'$1')[/color][/color]


        I don't know that this can be done in one line of code. I can do it
        in two lines. I'm too busy to write the syntax now, however, here is
        my idea.

        You can use regular expressions to SPLIT the original string into an
        array of strings, splitting by <.*> (in other words, splitting the
        string by <how> and <under> in your example, creating 3 substrings).

        Then it's a simple replace statement for each substring of the
        array.

        Comment

        • Dr John Stockton

          #5
          Re: help with string replace - for doing selective replace

          JRS: In article <beb94c5c.04082 00512.19960490@ posting.google. com>,
          dated Fri, 20 Aug 2004 06:12:36, seen in news:comp.lang. javascript,
          Prasad S <prasad_shir@re diffmail.com> posted :[color=blue]
          >
          >I wish to replace all the characters in a string except those which
          >are inside '<' & '>' characters. And there could be multiple
          >occurences of < & > within the string.[/color]

          Can the string be of the form " x < y < z > a > ? ", If so, some record
          of depth-of-quote is called for.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          Working...