Remove trailing newlines (blank lines) ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lihao0129@gmail.com

    Remove trailing newlines (blank lines) ???

    Hi, folks:

    I recently went through a strange problem with my Javascript code,
    say: I have a string variable which are from a 'textarea' element and
    I want to remove the trailing newlines inside the string. I am using
    something like the following:

    var txt = textarea_elemen t.value.replace (/\n*$/, '');

    But this replaced only the last newline(by changing '' to 'K', and
    alerting the response). Am I doing something wrong or is there any
    better ways to remove trailing black lines with Javascript? many
    thanks,

    lihao(XC)
  • lihao0129@gmail.com

    #2
    Re: Remove trailing newlines (blank lines) ???

    On Jan 27, 2:13 pm, "lihao0...@gmai l.com" <lihao0...@gmai l.comwrote:
    Hi, folks:
    >
    I recently went through a strange problem with my Javascript code,
    say: I have a string variable which are from a 'textarea' element and
    I want to remove the trailing newlines inside the string. I am using
    something like the following:
    >
       var txt = textarea_elemen t.value.replace (/\n*$/, '');
    >
    But this replaced only the last newline(by changing '' to 'K', and
    alerting the response). Am I doing something wrong or is there any
    better ways to remove trailing black lines with Javascript? many
    thanks,
    BTW. the scenario is to count the length of an input string without
    counting the trailing blank lines. thanks..

    lihao(XC)

    Comment

    • Evertjan.

      #3
      Re: Remove trailing newlines (blank lines)

      Thomas 'PointedEars' Lahn wrote on 27 jan 2008 in comp.lang.javas cript:
      lihao0129@gmail .com wrote:
      >[...] I have a string variable which are from a 'textarea' element
      >and I want to remove the trailing newlines inside the string.
      >
      I can see what trailing newlines and what newlines inside the string
      are. But what exactly are "trailing newlines inside the string"? IOW,
      what is it that they are trailing?
      >
      >I am using something like the following:
      >>
      > var txt = textarea_elemen t.value.replace (/\n*$/, '');
      >>
      >But this replaced only the last newline(by changing '' to 'K', and
      >alerting the response). [...]
      >
      AIUI, there are three problems with this approach:
      >
      1. The textarea value contains not (only) `\n' (LF), but (also) `\r\n'
      (CRLF). Replacing `\n' before the end of input would leave the
      `\r'.
      >
      2. There is at least one line that contains other whitespace
      characters
      followed by newline, for example "foo\n \n". In that case the
      `\n's would not be consecutive and so the expression would match
      only the last `\n'.
      >
      3. `\n*$' is inefficient as it would also match the empty string
      before the end of input whereas in fact the newline would be required
      for any replace to make sense.
      Eh?
      Therefore, try this:
      >
      var txt = textarea_elemen t.value.replace (/(\s*(\r?\n|\r)) +$/, '');
      Keep it simple, Thomas:

      var txt = textarea_elemen t.value.replace (/[\s\r\n]+$/, '');



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

      Comment

      • lihao0129@gmail.com

        #4
        Re: Remove trailing newlines (blank lines)

        On Jan 27, 3:56 pm, "lihao0...@gmai l.com" <lihao0...@gmai l.comwrote:
        On Jan 27, 2:38 pm, Thomas 'PointedEars' Lahn <PointedE...@we b.de>
        wrote:
        >
        >
        >
        >
        >
        lihao0...@gmail .com wrote:
        [...] I have a string variable which are from a 'textarea' element and
        I want to remove the trailing newlines inside the string.
        >
        I can see what trailing newlines and what newlines inside the string are..
        But what exactly are "trailing newlines inside the string"?  IOW, whatis
        it that they are trailing?
        >
        I am using something like the following:
        >
           var txt = textarea_elemen t.value.replace (/\n*$/, '');
        >
        But this replaced only the last newline(by changing '' to 'K', and
        alerting the response). [...]
        >
        AIUI, there are three problems with this approach:
        >
        1. The textarea value contains not (only) `\n' (LF), but (also) `\r\n'
           (CRLF).  Replacing `\n' before the end of input would leave the`\r'.
        >
        2. There is at least one line that contains other whitespace characters
           followed by newline, for example "foo\n  \n".  In that case the `\n's
           would not be consecutive and so the expression would match only the
           last `\n'.
        >
        3. `\n*$' is inefficient as it would also match the empty string before
           the end of input whereas in fact the newline would be required for any
           replace to make sense.
        >
        Therefore, try this:
        >
          var txt = textarea_elemen t.value.replace (/(\s*(\r?\n|\r)) +$/, '');
        >
        HTH
        >
        Hi, thanks all for your suggestions: -)
        >
        I've solved this problem by adding more newline patterns. since I need
        to count only the vertical whitespaces(not tabs, spaces), so I can not
        use /[\s\n\r]+$/.. The real purpose is to count the number of
        newlines(blank lines) at the end of the textarea. So I actually went
        with the following code:
        >
          var trailing_crs = textarea_elemen t.value.match(/(?:\r\n|\r|\n|
        \u0085|\u000C|\ u2028|\u2029)+$/);
          var num_crs = trailing_crs[0].length/2;
        Actually, in my application, it might be better to use: '*' instead of
        '+' in my regex pattern, otherwise I need to check trailing_crs
        before using it, say:

        var num_crs = trailing_crs ? trailing_crs[0].length/2 : 0;

        while with /(....)*$/, I can just use trailing_crs directly:

        var num_crs = trailing_crs[0].length/2;

        lihao(XC)

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Remove trailing newlines (blank lines)

          lihao0129@gmail .com wrote:
          On Jan 27, 3:56 pm, "lihao0...@gmai l.com" <lihao0...@gmai l.comwrote:
          >[...]
          Please trim your quotes:

          >I've solved this problem by adding more newline patterns. since I need
          >to count only the vertical whitespaces(not tabs, spaces), so I can not
          >use /[\s\n\r]+$/.. The real purpose is to count the number of
          >newlines(bla nk lines) at the end of the textarea. So I actually went
          >with the following code:
          >>
          > var trailing_crs = textarea_elemen t.value.match(/(?:\r\n|\r|\n|
          >\u0085|\u000C| \u2028|\u2029)+ $/);
          > var num_crs = trailing_crs[0].length/2;
          >
          Actually, in my application, it might be better to use: '*' instead of
          '+' in my regex pattern, otherwise I need to check trailing_crs
          before using it, say:
          >
          var num_crs = trailing_crs ? trailing_crs[0].length/2 : 0;
          >
          while with /(....)*$/, I can just use trailing_crs directly:
          >
          var num_crs = trailing_crs[0].length/2;
          The above code is error-prone, but I have no better solution as of yet other
          than not to use the unnecessary, not universally supported non-capturing
          parentheses.

          However,

          var num_nl = (s.match(...) || {0: ""})[0].length;

          works fine since JavaScript 1.3 (NN 4.0), JScript 3.0 (MSHTML 4.0),
          ECMAScript Ed. 3, so I don't think there is a need for inefficient
          pattern matching (`a*') only to work around the reference issue.




          PointedEars
          --
          var bugRiddenCrashP ronePieceOfJunk = (
          navigator.userA gent.indexOf('M SIE 5') != -1
          && navigator.userA gent.indexOf('M ac') != -1
          ) // Plone, register_functi on.js:16

          Comment

          Working...