Remove extra CrLf's from textarea

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

    #1

    Remove extra CrLf's from textarea

    I'd like to use regular expressions to remove extraneous Carriage Return
    Line Feeds (CrLf) from a textarea before the form is submitted.

    I'd like to remove all trailing CrLf's and convert all instances of 3
    consectutive CrLf's to just 2.

    I first escape() the textarea and match "%0D%0A" (i.e. CrLf).

    Here's what I've been testing; watch for word-wrap:

    <html>
    <head>
    <title>fix_crlf .htm</title>
    <script type="text/javascript">
    function fix() {
    var form = document.forms[0];
    var body = escape(form.Bod y.value);
    // try to remove all trailing CrLf's
    body = body.replace(/\%0D\%0A$/g,"");
    // try to convert all 3 CrLf's to 2
    body = body.replace(/\%0D\%0A\%0D\%0 A\%0D\%0A/g,"\%0D\%0A\%0D \%0A");
    body = unescape(body);
    // update the form field to see if it worked -- NOT!
    form.Body.value = body;
    }
    </script>
    </head>
    <body>
    <form>
    <textarea name="Body" rows="30"></textarea>
    <input type="button" value="fix()" onclick="fix()" >
    </form>
    </body>
    </html>


    I thought the "g" would handle multiple instances (of both cases) or do I
    have to do a "for" loop?

    Can anyone advise me? Thanks in advance.

    Note: JavaScript will be enabled as this is for a browser-based application
    that requires it.


  • Andrew Urquhart

    #2
    Re: Remove extra CrLf's from textarea

    McKirahan wrote:[color=blue]
    > I'd like to use regular expressions to remove extraneous Carriage
    > Return Line Feeds (CrLf) from a textarea before the form is submitted.
    >
    > I'd like to remove all trailing CrLf's and convert all instances of 3
    > consectutive CrLf's to just 2.[/color]
    <snip />

    strData = strData.replace (/(\r\n\s*){2,}/g, "\r\n\r\n") ;

    The above finds 2 or more CrLfs with optional whitespace between them
    and replaces it all with 2 CrLfs. If you're not bothered about the
    chance of whitespace then:

    strData = strData.replace (/(\r\n){3,}/g, "\r\n\r\n") ;

    [color=blue]
    > Note: JavaScript will be enabled as this is for a browser-based
    > application that requires it.[/color]

    For robustness do it after the form is submitted, i.e. on the server
    --
    Andrew Urquhart
    - FAQ: http://jibbering.com/faq
    - Archive: http://groups.google.com/groups?grou...ang.javascript
    - Reply: www.andrewu.co.uk/about/contact/


    Comment

    • McKirahan

      #3
      Re: Remove extra CrLf's from textarea

      "Andrew Urquhart" <reply@website. in.sig> wrote in message
      news:5nq2c.1680 $re1.1203@newsf e1-win...[color=blue]
      > McKirahan wrote:[color=green]
      > > I'd like to use regular expressions to remove extraneous Carriage
      > > Return Line Feeds (CrLf) from a textarea before the form is submitted.
      > >
      > > I'd like to remove all trailing CrLf's and convert all instances of 3
      > > consectutive CrLf's to just 2.[/color]
      > <snip />
      >
      > strData = strData.replace (/(\r\n\s*){2,}/g, "\r\n\r\n") ;
      >
      > The above finds 2 or more CrLfs with optional whitespace between them
      > and replaces it all with 2 CrLfs. If you're not bothered about the
      > chance of whitespace then:
      >
      > strData = strData.replace (/(\r\n){3,}/g, "\r\n\r\n") ;[/color]


      Thank you very much for your reply.

      But how would I remove all trailing (and leading) CrLf's?

      Thanks again.


      Comment

      • McKirahan

        #4
        Re: Remove extra CrLf's from textarea

        "McKirahan" <News@McKirahan .com> wrote in message
        news:juq2c.1917 09$uV3.791436@a ttbi_s51...[color=blue]
        > "Andrew Urquhart" <reply@website. in.sig> wrote in message
        > news:5nq2c.1680 $re1.1203@newsf e1-win...[color=green]
        > > McKirahan wrote:[color=darkred]
        > > > I'd like to use regular expressions to remove extraneous Carriage
        > > > Return Line Feeds (CrLf) from a textarea before the form is submitted.
        > > >
        > > > I'd like to remove all trailing CrLf's and convert all instances of 3
        > > > consectutive CrLf's to just 2.[/color]
        > > <snip />
        > >
        > > strData = strData.replace (/(\r\n\s*){2,}/g, "\r\n\r\n") ;
        > >
        > > The above finds 2 or more CrLfs with optional whitespace between them
        > > and replaces it all with 2 CrLfs. If you're not bothered about the
        > > chance of whitespace then:
        > >
        > > strData = strData.replace (/(\r\n){3,}/g, "\r\n\r\n") ;[/color]
        >
        >
        > Thank you very much for your reply.
        >
        > But how would I remove all trailing (and leading) CrLf's?
        >
        > Thanks again.[/color]


        Perhaps it would be?

        strData = strData.replace (/^(\r\n\s*)/g, "");
        strData = strData.replace (/(\r\n\s*)$/g, "");


        Comment

        • Andrew Urquhart

          #5
          Re: Remove extra CrLf's from textarea

          McKirahan wrote:[color=blue]
          > "McKirahan" <News@McKirahan .com> wrote in message
          > news:juq2c.1917 09$uV3.791436@a ttbi_s51...[color=green]
          >> But how would I remove all trailing (and leading) CrLf's?[/color]
          >
          > Perhaps it would be?
          >
          > strData = strData.replace (/^(\r\n\s*)/g, "");
          > strData = strData.replace (/(\r\n\s*)$/g, "");[/color]

          strData = strData.replace (/^[\r\n]+|[\r\n]+$/g, "");
          --
          Andrew Urquhart
          - FAQ: http://jibbering.com/faq
          - Archive: http://groups.google.com/groups?grou...ang.javascript
          - Reply: www.andrewu.co.uk/about/contact/


          Comment

          • McKirahan

            #6
            Re: Remove extra CrLf's from textarea

            "Andrew Urquhart" <reply@website. in.sig> wrote in message
            news:U%q2c.2496 $54.1875@newsfe 1-win...[color=blue]
            > McKirahan wrote:[color=green]
            > > "McKirahan" <News@McKirahan .com> wrote in message
            > > news:juq2c.1917 09$uV3.791436@a ttbi_s51...[color=darkred]
            > >> But how would I remove all trailing (and leading) CrLf's?[/color]
            > >
            > > Perhaps it would be?
            > >
            > > strData = strData.replace (/^(\r\n\s*)/g, "");
            > > strData = strData.replace (/(\r\n\s*)$/g, "");[/color]
            >
            > strData = strData.replace (/^[\r\n]+|[\r\n]+$/g, "");
            > --
            > Andrew Urquhart
            > - FAQ: http://jibbering.com/faq
            > - Archive: http://groups.google.com/groups?grou...ang.javascript
            > - Reply: www.andrewu.co.uk/about/contact/[/color]


            Thanks, Andrew.

            I recently bought a copy of O'Reilly's "Mastering Regular Expressions" which
            I'm wading through; someday I'll be self-sufficient (maybe).


            Comment

            Working...