By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,302 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

Remove trailing newlines (blank lines) ???

P: n/a
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 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Jan 27, 2:13*pm, "lihao0...@gmail.com" <lihao0...@gmail.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_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 '08 #2

P: n/a
Thomas 'PointedEars' Lahn wrote on 27 jan 2008 in comp.lang.javascript:
li*******@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_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)
Jan 27 '08 #3

P: n/a
On Jan 27, 3:56*pm, "lihao0...@gmail.com" <lihao0...@gmail.comwrote:
On Jan 27, 2:38*pm, Thomas 'PointedEars' Lahn <PointedE...@web.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_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 '08 #4

P: n/a
li*******@gmail.com wrote:
On Jan 27, 3:56 pm, "lihao0...@gmail.com" <lihao0...@gmail.comwrote:
>[...]
Please trim your quotes:
http://www.jibbering.com/faq/faq_not...s.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
Jan 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.