473,397 Members | 2,084 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,397 software developers and data experts.

count linebreaks

I need a quick way to count linebreaks. This is slug slow:

function findLinebreaks(str) {
var re='/\r\n|\n|\r\|\f/g';
var i=0;
while(re.match(str)){
i++;}
return i;
}

Cheers,
Jeff
Jul 23 '05 #1
3 3960
Ivo
"Jeff Thies" <no****@nospam.net> wrote in message
news:2%****************@newsread2.news.atl.earthli nk.net...
I need a quick way to count linebreaks. This is slug slow:

function findLinebreaks(str) {
var re='/\r\n|\n|\r\|\f/g';
var i=0;
while(re.match(str)){
i++;}
return i;
}

Cheers,
Jeff


What is wrong with
return str.split(/\r\n|\n|\r\|\f/)
possibly with an extra check for the last character on the last line?
HTH
Ivo
Jul 23 '05 #2


Jeff Thies wrote:
I need a quick way to count linebreaks. This is slug slow:

function findLinebreaks(str) {
var re='/\r\n|\n|\r\|\f/g';
var i=0;
while(re.match(str)){
i++;}
return i;
}


What happends when you use a regular expression literal and only the
test method:

function countLineBreaks1 (string) {
var pattern = /\r\n|\r|\n/g;
var count = 0;
while (pattern.test(string)) {
count++;
}
return count;
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #3
The winner is:

return str.split(/\n|\f/).length;

much much much faster.

The \r\n part of my test caused problems with IE not counting blank lines.
The \f is for Macs.

Cheers,
Jeff


Jeff Thies wrote:
I need a quick way to count linebreaks. This is slug slow:

function findLinebreaks(str) {
var re='/\r\n|\n|\r\|\f/g';
var i=0;
while(re.match(str)){
i++;}
return i;
}


What happends when you use a regular expression literal and only the
test method:

function countLineBreaks1 (string) {
var pattern = /\r\n|\r|\n/g;
var count = 0;
while (pattern.test(string)) {
count++;
}
return count;
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #4

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

Similar topics

13
by: Tomba | last post by:
hi there, I am looking for a way to convert line breaks that are written in a textarea (with an enter) to <br> to create the same line break in html is there anyone who can help me with this?...
5
by: Mechphisto | last post by:
I'm using a textarea to insert some notes into a database. It's inserting the complete result into the mySQL table including the linebreaks...and not as a visible symbol but an actual linebreak. If...
2
by: Stefan Landgraf | last post by:
Hi there! How can i replace linebreaks with <br> in datalist output ? Is there a way i can do that in the aspx page itself without having to go through the code page? Thanks Stefan
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
0
by: michga | last post by:
Hello, Using docbook 4.4, docbook-xsl 1.70.1 and fop, I've successfully used this snippet of code in a customized-layer stylesheet to insert one linebreak: <xsl:template...
7
by: MrNobody | last post by:
I'm trying to do some regex in C# but for some reason linebreaks are causing my regex to not work. the test string goes like this: string ss = "<tagname...
2
by: peter | last post by:
I have a xslt that writes a txt file from xml. Xml file is generated by a user input on a webpage and some nodes have linebreaks which corrupts my output. is there a way to generally set that xslt...
1
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I want to match some HTML string using Regex but the linebreaks are getting me. Is there a way to just completely ignore linebreaks in my regular expression? If not, how would I specify a...
5
by: rweth | last post by:
I am using nntplib to download archived xml messages from our internal newsgroup. This is working fine except the download of files to the connected server, has extra embedded lines in them (all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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...
0
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,...
0
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...

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.