Remove trailing newlines (blank lines) ???

Discussion in 'Javascript' started by lihao0129@gmail.com, Jan 27, 2008.

  1. Guest

    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_element.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)
    , Jan 27, 2008
    #1
    1. Advertising

  2. Guest

    On Jan 27, 2:13 pm, "" <> wrote:
    > 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_element.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)
    , Jan 27, 2008
    #2
    1. Advertising

  3. Re: Remove trailing newlines (blank lines)

    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_element.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_element.value.replace(/(\s*(\r?\n|\r))+$/, '');


    HTH

    PointedEars
    --
    Anyone who slaps a 'this page is best viewed with Browser X' label on
    a Web page appears to be yearning for the bad old days, before the Web,
    when you had very little chance of reading a document written on another
    computer, another word processor, or another network. -- Tim Berners-Lee
    Thomas 'PointedEars' Lahn, Jan 27, 2008
    #3
  4. Evertjan. Guest

    Re: Remove trailing newlines (blank lines)

    Thomas 'PointedEars' Lahn wrote on 27 jan 2008 in comp.lang.javascript:

    > 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_element.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_element.value.replace(/(\s*(\r?\n|\r))+$/, '');


    Keep it simple, Thomas:

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



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
    Evertjan., Jan 27, 2008
    #4
  5. Guest

    Re: Remove trailing newlines (blank lines)

    On Jan 27, 2:38 pm, Thomas 'PointedEars' Lahn <>
    wrote:
    > 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_element.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_element.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_element.value.match(/(?:\r\n|\r|\n|
    \u0085|\u000C|\u2028|\u2029)+$/);
    var num_crs = trailing_crs[0].length/2;

    I am not quite sure why I need to divided trailing_crs[0].length by 2,
    maybe the black lines are '\r\n' things?? I got this by alert()ing and
    then analyzing the resules. it looks work well for my current browser,
    but not sure if it is robust for other browsers though..

    Are there some robust ways in Javascript that I can count number of
    trailing newlines?

    many thanks,
    lihao(XC)
    , Jan 27, 2008
    #5
  6. Guest

    Re: Remove trailing newlines (blank lines)

    On Jan 27, 3:56 pm, "" <> wrote:
    > On Jan 27, 2:38 pm, Thomas 'PointedEars' Lahn <>
    > wrote:
    >
    >
    >
    >
    >
    > > 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_element.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_element.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_element.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)
    , Jan 27, 2008
    #6
  7. Re: Remove trailing newlines (blank lines)

    wrote:
    > On Jan 27, 3:56 pm, "" <> wrote:
    >> [...]


    Please trim your quotes:
    http://www.jibbering.com/faq/faq_notes/clj_posts.html#ps1Post

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

    http://PointedEars.de/es-matrix/


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
    Thomas 'PointedEars' Lahn, Jan 27, 2008
    #7
    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. Peter Otten

    Re: how to remove the blank lines?

    Peter Otten, Dec 8, 2006, in forum: Python
    Replies:
    1
    Views:
    637
    Peter Otten
    Dec 9, 2006
  2. Russell Warren

    logging module and trailing newlines

    Russell Warren, Oct 2, 2007, in forum: Python
    Replies:
    2
    Views:
    270
    Russell Warren
    Oct 3, 2007
  3. Dotan Cohen
    Replies:
    2
    Views:
    504
    Dotan Cohen
    Oct 10, 2011
  4. John

    Removing trailing newlines -

    John, Apr 23, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    167
    J. Gleixner
    Apr 24, 2008
  5. Cah Sableng
    Replies:
    0
    Views:
    236
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page