replace *except* where...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Mark Bram

    replace *except* where...

    Hi All,

    I have the following to replace newline chars with <br> in a string:

    ..replace(/\n/g,"<br>")

    How can I change this so that it replaces only if there is not already a
    "<br>newlin e" or "newline<p> " combo?

    Thanks for any advice!

    Rob
    :)


  • Evertjan.

    #2
    Re: replace *except* where...

    Robert Mark Bram wrote on 27 sep 2004 in comp.lang.javas cript:[color=blue]
    > I have the following to replace newline chars with <br> in a string:
    >
    > .replace(/\n/g,"<br>")
    >
    > How can I change this so that it replaces only if there is not already a
    > "<br>newlin e" or "newline<p> " combo?[/color]

    if(/#$#$#$#/.test(t))alert( "error")

    t=t.replace(/\n/g,"#$#$#$#")
    ..replace(/#$#$#$#<p>/g,"\n<p>")
    ..replace(/<br>#$#$#$#/g,"<br>\n")
    ..replace(/#$#$#$#/g,"<br>")

    // all replaces on one line please !!

    not tested

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

    Comment

    • Robert Mark Bram

      #3
      Re: replace *except* where...

      Hello Evertjan!
      [color=blue][color=green]
      > > .replace(/\n/g,"<br>")
      > > How can I change this so that it replaces only if there is not already a
      > > "<br>newlin e" or "newline<p> " combo?[/color]
      >
      > if(/#$#$#$#/.test(t))alert( "error")
      >
      > t=t.replace(/\n/g,"#$#$#$#")
      > .replace(/#$#$#$#<p>/g,"\n<p>")
      > .replace(/<br>#$#$#$#/g,"<br>\n")
      > .replace(/#$#$#$#/g,"<br>")
      >
      > // all replaces on one line please !![/color]

      #$#$#$# seemed to cause a few problems. Instead of #$#$#$# I used jxjxjxjx
      and it works well - thank you very much!

      Rob
      :)


      Comment

      • Duncan Booth

        #4
        Re: replace *except* where...

        Robert Mark Bram wrote:
        [color=blue]
        > I have the following to replace newline chars with <br> in a string:
        >
        > .replace(/\n/g,"<br>")
        >
        > How can I change this so that it replaces only if there is not already a
        > "<br>newlin e" or "newline<p> " combo?
        >[/color]

        So long as you don't mind losing the unwanted newlines, the simplest thing
        would be to remove them before replacing the ones you do want e.g.:

        s.replace(/\n*(<(br|p)>)\n */g, '$1').replace(/\n/g,'<br>')

        (split the regex up if you don't want to catch newline<br> and <p>newline
        also.)

        Comment

        • Grant Wagner

          #5
          Re: replace *except* where...

          Robert Mark Bram wrote:
          [color=blue]
          > Hi All,
          >
          > I have the following to replace newline chars with <br> in a string:
          >
          > .replace(/\n/g,"<br>")
          >
          > How can I change this so that it replaces only if there is not already a
          > "<br>newlin e" or "newline<p> " combo?[/color]

          Although the end tag for a paragraph is listed as optional <url:
          http://www.w3.org/TR/REC-html40/struct/text.html#edef-P />, you should get
          used to coding a paragraph as:

          <p>Paragraph text</p>

          rather than:

          Paragraph text<p>

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq

          Comment

          Working...