Remove trailing newlines (blank lines) ???

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

  1. lihao0129

    lihao0129 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

    lihao0129, Jan 27, 2008
    1. Advertisements

  2. lihao0129

    lihao0129 Guest

    BTW. the scenario is to count the length of an input string without
    counting the trailing blank lines. thanks..

    lihao0129, Jan 27, 2008
    1. Advertisements

  3. 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?
    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))+$/, '');


    Thomas 'PointedEars' Lahn, Jan 27, 2008
  4. lihao0129

    Evertjan. Guest

    Thomas 'PointedEars' Lahn wrote on 27 jan 2008 in comp.lang.javascript:
    Keep it simple, Thomas:

    var txt = textarea_element.value.replace(/[\s\r\n]+$/, '');
    Evertjan., Jan 27, 2008
  5. lihao0129

    lihao0129 Guest

    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|
    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,
    lihao0129, Jan 27, 2008
  6. lihao0129

    lihao0129 Guest

    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;

    lihao0129, Jan 27, 2008
  7. Please trim your quotes:
    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


    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.

    Thomas 'PointedEars' Lahn, Jan 27, 2008
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.