473,395 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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_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
4 22283
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: ted | last post by:
I'm having trouble using the re module to remove empty lines in a file. Here's what I thought would work, but it doesn't: import re f = open("old_site/index.html") for line in f: line =...
2
by: James | last post by:
below is some codes. my arraylist below reads from a file. My files contains blank line (ie carriage return) My message dialog shows all strings being captured. However i do not want my...
5
by: micklee74 | last post by:
hi i have a file test.dat eg abcdefgh ijklmn <-----newline opqrs tuvwxyz
7
by: Bosconian | last post by:
I know that str.replace(/^\s+|\s+$/g,''); will trim a string of space, but what about removing extra spaces from the middle? Where "hello world"
2
by: Olveres | last post by:
Hi, I have managed to work out how to add new lines into a calculated text box. However in this text box some of the outcome fields are empty however when previewing the report it includes the...
6
by: Phil Endecott | last post by:
Dear Experts, I have some Javascript code that reads and sometimes sets the content of a textarea. I want this to be reasonably browser and platform independent. My question is, what...
3
by: Paul | last post by:
Hi, My RichTextBox has multiple lines of text. Most of the lines unfortunately end with a space. Is it possible to replace the space and NewLine/Line Feed with just the NewLine/LineFeed? So...
6
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
2
by: Russell Warren | last post by:
I was just setting up some logging in a make script and decided to give the built-in logging module a go, but I just found out that the base StreamHandler always puts a newline at the end of each...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.