Regular Expression help: multi-line mode

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

    Regular Expression help: multi-line mode

    I wish to remove the last newline (if any) from a string.
    This is where I started:

    var text = "hello\n";
    text = text.replace( /^(.*)\n?$/, "$1" );

    This seems to work, but not if the test has embedded newlines:
    text = "hello\ngoo d-bye\n";

    I've tried a few variations but no luck so far.
    (I worked around the problem by testing if the
    last character == '\n' and if so use text.slice(0,-1)
    to remove it.)

    Can someone provide me the proper regex for this?
    Thanks!

    -Wayne
  • Martin Honnen

    #2
    Re: Regular Expression help: multi-line mode

    Wayne wrote:
    I wish to remove the last newline (if any) from a string.
    The do
    string = string.replace(/\n$/, '');


    --

    Martin Honnen

    Comment

    • Anthony Levensalor

      #3
      Re: Regular Expression help: multi-line mode

      Wayne said:
      Can someone provide me the proper regex for this?
      Thanks!
      >
      -Wayne
      Wayne,

      I extrapolated it out to a separate call, and it worked for me.

      alert(str.repla ce("\n"), "$1");

      ~A!

      --
      Anthony Levensalor
      anthony@mypetpr ogrammer.com

      Only two things are infinite, the universe and human stupidity,
      and I'm not sure about the former. - Albert Einstein

      Comment

      • Evertjan.

        #4
        Re: Regular Expression help: multi-line mode

        Martin Honnen wrote on 29 dec 2007 in comp.lang.javas cript:
        Wayne wrote:
        >I wish to remove the last newline (if any) from a string.
        >
        The do
        string = string.replace(/\n$/, '');
        Non regex variation:

        var temp = str.split('\n') ;
        if (!temp.pop()) str = temp.join('\n') ;



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

        Comment

        • Evertjan.

          #5
          Re: Regular Expression help: multi-line mode

          Anthony Levensalor wrote on 29 dec 2007 in comp.lang.javas cript:
          Wayne said:
          >
          >Can someone provide me the proper regex for this?
          >Thanks!
          >>
          >-Wayne
          Wayne,
          >
          I extrapolated it out to a separate call, and it worked for me.
          >
          alert(str.repla ce("\n"), "$1");
          I am sure you did not test that!

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

          Comment

          • Anthony Levensalor

            #6
            Re: Regular Expression help: multi-line mode

            Evertjan. said:
            [snip]
            I am sure you did not test that!
            >
            I did, and it removed the newlines. Looking at it right now, though, I
            see an obvious parens issue, how the hell did that even happen? Well,
            even with the parens screwed up, it didn't toss any errors, and it
            pulled out the newlines I stuck into a string.

            --
            Anthony Levensalor
            anthony@mypetpr ogrammer.com

            Only two things are infinite, the universe and human stupidity,
            and I'm not sure about the former. - Albert Einstein

            Comment

            • Evertjan.

              #7
              Re: Regular Expression help: multi-line mode

              Anthony Levensalor wrote on 29 dec 2007 in comp.lang.javas cript:
              Evertjan. said:
              [snip]
              >I am sure you did not test that!
              >>
              I did, and it removed the newlines. Looking at it right now, though, I
              see an obvious parens issue, how the hell did that even happen? Well,
              even with the parens screwed up, it didn't toss any errors, and it
              pulled out the newlines I stuck into a string.
              But that was not the OQ.

              Then even it did only work,
              because of the strange compatibility of alert().



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

              Comment

              • Wayne

                #8
                Re: Regular Expression help: multi-line mode

                Martin Honnen wrote:
                Wayne wrote:
                >I wish to remove the last newline (if any) from a string.
                >
                The do
                string = string.replace(/\n$/, '');
                >
                >
                So simple! That's what I get for working through the night,
                on a weekend to boot! Thanks!

                -Wayne

                Comment

                • Anthony Levensalor

                  #9
                  Re: Regular Expression help: multi-line mode

                  Evertjan. said:
                  [snip]
                  Then even it did only work,
                  because of the strange compatibility of alert().
                  I am 100% certain that's the only way it could have possibly worked with
                  so obvious a syntax error in it. I've been awake for three days and
                  haven't really left my desk, so if I missed a parenthesis issue, then I
                  missed it. Oh friggin' well.

                  ~A!

                  --
                  Anthony Levensalor
                  anthony@mypetpr ogrammer.com

                  Only two things are infinite, the universe and human stupidity,
                  and I'm not sure about the former. - Albert Einstein

                  Comment

                  • Wayne

                    #10
                    Re: Regular Expression help: multi-line mode

                    Evertjan. wrote:
                    Martin Honnen wrote on 29 dec 2007 in comp.lang.javas cript:
                    >
                    >Wayne wrote:
                    >>I wish to remove the last newline (if any) from a string.
                    >The do
                    > string = string.replace(/\n$/, '');
                    >
                    Non regex variation:
                    >
                    var temp = str.split('\n') ;
                    if (!temp.pop()) str = temp.join('\n') ;
                    >
                    >
                    >

                    Thanks! I know there must be a million ways,
                    but I am looking for one that is efficient.
                    It seems to me this would be slower (and take more
                    memory) than Martin's RegExp.

                    -Wayne

                    Comment

                    • Evertjan.

                      #11
                      Re: Regular Expression help: multi-line mode

                      Wayne wrote on 29 dec 2007 in comp.lang.javas cript:
                      Evertjan. wrote:
                      >Martin Honnen wrote on 29 dec 2007 in comp.lang.javas cript:
                      >>
                      >>Wayne wrote:
                      >>>I wish to remove the last newline (if any) from a string.
                      >>The do
                      >> string = string.replace(/\n$/, '');
                      >>
                      >Non regex variation:
                      >>
                      > var temp = str.split('\n') ;
                      > if (!temp.pop()) str = temp.join('\n') ;
                      >>
                      >
                      Thanks! I know there must be a million ways,
                      but I am looking for one that is efficient.
                      It seems to me this would be slower (and take more
                      memory) than Martin's RegExp.
                      I doubt that, in the case of the absence of the final \n,
                      as in the regex case the string is still duplicated twice,
                      once to an internal temporary location and then back
                      to the variable's string space. It is nearly as bad as string
                      concatenation.

                      So if speed were at a premium [which I doubt to be the case]
                      better do this:

                      if (/\n$/.test(string))
                      string = string.replace(/\n$/, '');



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

                      Comment

                      Working...