Remove trailing newlines (blank lines) ???

L

lihao0129

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)
 
L

lihao0129

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)
 
T

Thomas 'PointedEars' Lahn

[...] 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
 
E

Evertjan.

Thomas 'PointedEars' Lahn wrote on 27 jan 2008 in comp.lang.javascript:
[...] 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]+$/, '');
 
L

lihao0129

[...] 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)
 
L

lihao0129

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

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)
 
T

Thomas 'PointedEars' Lahn


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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top