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

count words

I have some innerHTML stored as a string, and I would like to know how
many time throughout the whole innerHTML the occurence of "Hello<BR>"
occurs. I was wondering if RegEx or some elequant method exists?

Thanks,
Yin
For example: InnerHtml = <b>thisis Hello<BR>
&npsb<script>Alert("Test")</script>Hello<BR>

I would like to use Javascript to parse this and return me answer of
2, since the string
Hello<BRoccurs exactly twice.
Jun 27 '08 #1
6 1242
Yin99 wrote:
I have some innerHTML stored as a string, and I would like to know how
many time throughout the whole innerHTML the occurence of "Hello<BR>"
occurs. I was wondering if RegEx or some elequant method exists?

Thanks,
Yin
For example: InnerHtml = <b>thisis Hello<BR>
&npsb<script>Alert("Test")</script>Hello<BR>

I would like to use Javascript to parse this and return me answer of
2, since the string
Hello<BRoccurs exactly twice.
innerHTML.match(/Hello\<BR\>/g).length

Jun 27 '08 #2
Dan Rumney wrote:
Yin99 wrote:
>I have some innerHTML stored as a string, and I would like to know how
many time throughout the whole innerHTML the occurence of "Hello<BR>"
occurs. I was wondering if RegEx or some elequant method exists?
[...]

For example: InnerHtml = <b>thisis Hello<BR>
&npsb<script>Alert("Test")</script>Hello<BR>

I would like to use Javascript to parse this and return me answer of
2, since the string
Hello<BRoccurs exactly twice.

innerHTML.match(/Hello\<BR\>/g).length
`<' and `>' do not need to be escaped. Also, if there is no match,
String.prototype.match() will return `null' and `null' has no properties
which is why the `length' property access would result in a TypeError.
Therefore:

(InnerHtml.match(/Hello<BR>/g) || "").length

(ECMAScript identifiers are case-sensitive.)
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jun 27 '08 #3
On Jun 5, 5:46 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Dan Rumney wrote:
Yin99 wrote:
[deleted]
innerHTML.match(/Hello\<BR\>/g).length

`<' and `>' do not need to be escaped. Also, if there is no match,
String.prototype.match() will return `null' and `null' has no properties
which is why the `length' property access would result in a TypeError.
Therefore:

(InnerHtml.match(/Hello<BR>/g) || "").length
Wouldn't that return a count of 1 when it didn't match anything?
Jun 27 '08 #4
david.karr wrote:
[snip]
>Therefore:

(InnerHtml.match(/Hello<BR>/g) || "").length

Wouldn't that return a count of 1 when it didn't match anything?
No

(Checked with Firebug)

alert("".length) displays '0'
Jun 27 '08 #5
david.karr wrote:
Thomas 'PointedEars' Lahn wrote:
>Dan Rumney wrote:
>>Yin99 wrote:
[deleted]
innerHTML.match(/Hello\<BR\>/g).length
`<' and `>' do not need to be escaped. Also, if there is no match,
String.prototype.match() will return `null' and `null' has no properties
which is why the `length' property access would result in a TypeError.
Therefore:

(InnerHtml.match(/Hello<BR>/g) || "").length

Wouldn't that return a count of 1 when it didn't match anything?
My Magic 8 Ball says: Unlikely.

(Since when has the empty string a length of 1 instead of 0?)
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #6
In comp.lang.javascript message <0f8eb53f-534e-49f1-90c4-2ab9e7f9e26a@2g
2000hsn.googlegroups.com>, Thu, 5 Jun 2008 15:09:23, Yin99
<ws@ziowave.composted:
>I have some innerHTML stored as a string, and I would like to know how
many time throughout the whole innerHTML the occurence of "Hello<BR>"
occurs. I was wondering if RegEx or some elequant method exists?
One way is to use D.replace(/Hello<BR>/g) and see how much shorter
that is.

Another : S.match(/(Hello<BR>)/g).length - if the string varies,
use new RegExp rather than a literal.

If using .split("Hello<BR>") you may need to be concerned about the
possibility of it being first or last, and which browser is in use.

See also in <URL:http://www.merlyn.demon.co.uk/js-valid.htm#SSRA>.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jun 27 '08 #7

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

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
11
by: Foodbank | last post by:
Hello, I'm trying to develop a program that will enable me to count the number of words in a text file. As a plus, I'd like to be able to count how many different words there are too. I have a...
9
by: cw bebop | last post by:
Hi all Using Visual Studio C# Have a string string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck." ******
9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
4
by: jackloanshark | last post by:
Hello, I am a beginner of c. I wrote a program about counting words in a txt file.But actually there are some error in it and it will stop counting at the first paragraph and just return the number...
7
by: supriyanaidu | last post by:
Hi i am new to this forum . i am doing small project in that i want to count the number of words in the given .for example in my give file the content will be in this format Firstname, last...
3
by: frozz85 | last post by:
I need to search through the file and look for lines that begin with "From". I need to parse the From line and print out the second word for each From line and then also count the number of From...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.