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) 4 22352
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)
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)
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) 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 ?
|
by: micklee74 |
last post by:
hi
i have a file test.dat eg
abcdefgh
ijklmn
<-----newline
opqrs
tuvwxyz
|
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"
|
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...
| |
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...
|
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?
|
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'
|
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...
|
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...
|
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,...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |