473,748 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_elemen t.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 22345
On Jan 27, 2:13*pm, "lihao0...@gmai l.com" <lihao0...@gmai l.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_elemen t.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.javas cript:
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_elemen t.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_elemen t.value.replace (/(\s*(\r?\n|\r)) +$/, '');
Keep it simple, Thomas:

var txt = textarea_elemen t.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...@gmai l.com" <lihao0...@gmai l.comwrote:
On Jan 27, 2:38*pm, Thomas 'PointedEars' Lahn <PointedE...@we b.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_elemen t.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_elemen t.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_elemen t.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...@gmai l.com" <lihao0...@gmai l.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(bla nk lines) at the end of the textarea. So I actually went
with the following code:

var trailing_crs = textarea_elemen t.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 bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.js:16
Jan 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
23512
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 = re.sub(r'^\s+$|\n', '', line) print line
2
6519
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 array to contain "blank" string/line/carriage return How do i remove the index so that my arraylist become smaller ?
5
20874
by: micklee74 | last post by:
hi i have a file test.dat eg abcdefgh ijklmn <-----newline opqrs tuvwxyz
7
16476
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
3996
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 blank fields, so each section of the report is the same size, my field is set to can grow/shrink, but I think my inclusion in the code for the calculated box of all 15 outcomes (I have no choice) is what's causing each calculated box to be the...
6
2693
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 characters should I expect to find at the end of a line? I suspect that I need to cope with either "\n" or "\r\n"; can someone confirm? Setting the content is more of a challenge. I don't want to have nasty browser detection to select what to use for...
3
5839
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 in essence just removing the trailing space from each line?
6
18407
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
8037
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 log. There is a comment in the code that says "The record is then written to the stream with a trailing newline "... I guess there wasn't the feedback to drive the change. All I'm after is the ability to log things like... Compiling...
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.