RegExp for remove all trailing CrLf's?

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

    #1

    RegExp for remove all trailing CrLf's?

    How would I use a regular expression to remove all trailing Carriage Returns
    and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.

    Also, are they any great references for learning how to use Regular
    Expressions?


  • Brian Genisio

    #2
    Re: RegExp for remove all trailing CrLf's?

    McKirahan wrote:[color=blue]
    > How would I use a regular expression to remove all trailing Carriage Returns
    > and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.
    >
    > Also, are they any great references for learning how to use Regular
    > Expressions?
    >
    >[/color]

    Ok, here is an example:
    /////////////////////////////////////////////////////////////////////
    var text = "Test\nWith\nSo me\nCarriageRet urns\rAnd\nLine Feeds\r\n";
    alert(text);

    var text2 = text.replace(/[\r\n]/g, "");
    alert(text2);
    /////////////////////////////////////////////////////////////////////

    Here is what it is saying... Find a pattern with a single character of
    \r (Carriage Return) or \n (New lines), and replace it with an empty
    string. the /.../ slashes encapsulate the RegEx, and the g at the end
    is a command to the RegEx reader to do it for all matches, not just the
    first one.

    In effect, all \r and \n characters are removed completely...

    As for a reference for Regular Expressions, I know that OReily has a
    grea book on them... I am not sure about online references. I haven't
    needed a reference in so long, because once I learned them, I found
    places to use them all the time... in editors, or in programing, so I
    never forgot them. (That is my plug to encourage you to learn them)

    Brian

    Comment

    • McKirahan

      #3
      Re: RegExp for remove all trailing CrLf's?

      "Brian Genisio" <BrianGenisio@y ahoo.com> wrote in message
      news:4018fde4$1 @10.10.0.241...[color=blue]
      > McKirahan wrote:[color=green]
      > > How would I use a regular expression to remove all trailing Carriage[/color][/color]
      Returns[color=blue][color=green]
      > > and Line Feeds (%0D%0A) from a textarea's value? Thanks in advance.
      > >
      > > Also, are they any great references for learning how to use Regular
      > > Expressions?
      > >
      > >[/color]
      >
      > Ok, here is an example:
      > /////////////////////////////////////////////////////////////////////
      > var text = "Test\nWith\nSo me\nCarriageRet urns\rAnd\nLine Feeds\r\n";
      > alert(text);
      >
      > var text2 = text.replace(/[\r\n]/g, "");
      > alert(text2);
      > /////////////////////////////////////////////////////////////////////
      >
      > In effect, all \r and \n characters are removed completely...[/color]


      Thank you for your reply.

      I had asked how to remove only trailing CrLf's not all of them.

      I'm also interested in how to remove all leading CrLf's.

      The next level would be to replace all embedded instances of 3 consecutive
      CrLf's with 2.

      The purpose of this is to clean up "textarea" input as it may contain
      extraneous CrLf's.

      P.S. I know the basics of Regular Expressions, I'm curious as where to
      learn the advanced stuff; I'll look at O'Reilly.


      Comment

      • McKirahan

        #4
        Re: RegExp for remove all trailing CrLf's?

        > I had asked how to remove only trailing CrLf's not all of them.[color=blue]
        >
        > I'm also interested in how to remove all leading CrLf's.
        >
        > The next level would be to replace all embedded instances of 3 consecutive
        > CrLf's with 2.
        >
        > The purpose of this is to clean up "textarea" input as it may contain
        > extraneous CrLf's.[/color]


        I posted this more detailed question ("RegExp removing extraneous CrLf's?")
        at "microsoft.publ ic.scripting.vb script" and received the following solution
        from "Steve Fulton":

        With New RegExp
        .Pattern = "^(?:\r\n)+|(?: \r\n)+$|((\r\n) \2)\2+"
        .Global = True
        .MultiLine = False
        NewString = .Replace(OldStr ing, "$1")
        End With


        Below is my JavaScript version of it:

        <html>
        <head>
        <title>textarea .htm</title>
        <script type="text/javascript">
        function crlf() {
        var form = document.forms[0];
        var data = form.data.value ;
        var regx = new RegExp("^(?:\r\ n)+|(?:\r\n)+$| ((\r\n)\2)\2+") ;
        regx.global = true;
        regx.multiline = false;
        var temp = data.replace(re gx,"$1");
        temp = temp.replace(re gx,"$1");
        alert(data.leng th + " : " + temp.length);
        form.data.value = temp;
        }
        </script>
        </head>
        <body>
        <form>
        <textarea name="data" cols="50" rows="10"></textarea>
        <br><input type="button" value="OK" onclick="crlf() ">
        </form>
        </body>
        </html>



        Any idea why I have to add a second
        temp = temp.replace(re gx,"$1");
        to get it to remove trailing CrLf's?
        I'll ask "Steve Fulton"...


        Comment

        • rh

          #5
          Re: RegExp for remove all trailing CrLf's?

          "McKirahan" <News@McKirahan .com> wrote:


          <snip>[color=blue]
          > Below is my JavaScript version of it:
          >
          > <html>
          > <head>
          > <title>textarea .htm</title>
          > <script type="text/javascript">
          > function crlf() {
          > var form = document.forms[0];
          > var data = form.data.value ;
          > var regx = new RegExp("^(?:\r\ n)+|(?:\r\n)+$| ((\r\n)\2)\2+") ;
          > regx.global = true;
          > regx.multiline = false;
          > var temp = data.replace(re gx,"$1");
          > temp = temp.replace(re gx,"$1");
          > alert(data.leng th + " : " + temp.length);
          > form.data.value = temp;
          > }
          > </script>
          > </head>
          > <body>
          > <form>
          > <textarea name="data" cols="50" rows="10"></textarea>
          > <br><input type="button" value="OK" onclick="crlf() ">
          > </form>
          > </body>
          > </html>
          >
          >
          >
          > Any idea why I have to add a second
          > temp = temp.replace(re gx,"$1");
          > to get it to remove trailing CrLf's?
          > I'll ask "Steve Fulton"...[/color]

          Most likely because, while "global" is a property of RegExps, it is
          read-only. The way to set it is to provide it as an argument to the
          RegExp constructor.

          See
          <url: http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/>

          Also, your converted rendition has a problem in that it will not
          remove the embedded cr/lf's. That's because escapes ("\"s) must be
          doubled in a string in order that they not cause conversion of the
          character following to a normalized string value. That's OK for "\r"
          and "\n", but the \2 back-reference will not survive as such in the
          generated regular expression.

          When working with non-literal RegExp's, it's often a good idea to
          print the RegExp following construction, e.g.,

          alert(regx.toSt ring());

          to ensure it's formed as intended.

          Also note that some browsers, e.g., Netscape, treat the replacement
          string "$1" as a literal, rather than null, if it hasn't been assigned
          a value during the pattern match execution at the time of
          substitution.

          And finally, the RegExp supplied reduces 3 or more \r\n's to 2, which
          isn't what you stated in the request, but nonetheless may be what you
          wanted.

          ../rh

          Comment

          Working...