RegExp for remove all trailing CrLf's?

Discussion in 'Javascript' started by McKirahan, Jan 28, 2004.

  1. McKirahan

    McKirahan Guest

    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?
     
    McKirahan, Jan 28, 2004
    #1
    1. Advertising

  2. McKirahan wrote:
    > 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?
    >
    >


    Ok, here is an example:
    /////////////////////////////////////////////////////////////////////
    var text = "Test\nWith\nSome\nCarriageReturns\rAnd\nLineFeeds\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
     
    Brian Genisio, Jan 29, 2004
    #2
    1. Advertising

  3. McKirahan

    McKirahan Guest

    "Brian Genisio" <> wrote in message
    news:4018fde4$1@10.10.0.241...
    > McKirahan wrote:
    > > 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?
    > >
    > >

    >
    > Ok, here is an example:
    > /////////////////////////////////////////////////////////////////////
    > var text = "Test\nWith\nSome\nCarriageReturns\rAnd\nLineFeeds\r\n";
    > alert(text);
    >
    > var text2 = text.replace(/[\r\n]/g, "");
    > alert(text2);
    > /////////////////////////////////////////////////////////////////////
    >
    > In effect, all \r and \n characters are removed completely...



    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.
     
    McKirahan, Jan 29, 2004
    #3
  4. McKirahan

    McKirahan Guest

    > 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.



    I posted this more detailed question ("RegExp removing extraneous CrLf's?")
    at "microsoft.public.scripting.vbscript" 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(OldString, "$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(regx,"$1");
    temp = temp.replace(regx,"$1");
    alert(data.length + " : " + 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(regx,"$1");
    to get it to remove trailing CrLf's?
    I'll ask "Steve Fulton"...
     
    McKirahan, Jan 29, 2004
    #4
  5. McKirahan

    rh Guest

    "McKirahan" <> wrote:


    <snip>
    > 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(regx,"$1");
    > temp = temp.replace(regx,"$1");
    > alert(data.length + " : " + 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(regx,"$1");
    > to get it to remove trailing CrLf's?
    > I'll ask "Steve Fulton"...


    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.netscape.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.toString());

    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
     
    rh, Jan 30, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Donald Canton

    remove trailing whitespace from string

    Donald Canton, Feb 9, 2004, in forum: C++
    Replies:
    5
    Views:
    11,219
    Tilman Kuepper
    Feb 9, 2004
  2. pd
    Replies:
    3
    Views:
    469
    Jeff Higgins
    Dec 7, 2007
  3. Joao Silva
    Replies:
    16
    Views:
    379
    7stud --
    Aug 21, 2009
  4. McKirahan

    Remove extra CrLf's from textarea

    McKirahan, Mar 6, 2004, in forum: Javascript
    Replies:
    3
    Views:
    555
    McKirahan
    Mar 6, 2004
  5. Replies:
    3
    Views:
    194
Loading...

Share This Page