count words

Y

Yin99

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<BR> occurs exactly twice.
 
T

Thomas 'PointedEars' Lahn

Dan said:
Yin99 said:
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<BR> occurs 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
 
D

david.karr

Dan said:
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?
 
T

Thomas 'PointedEars' Lahn

david.karr said:
Thomas said:
Dan said:
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
 
D

Dr J R Stockton

In comp.lang.javascript message <0f8eb53f-534e-49f1-90c4-2ab9e7f9e26a@2g
2000hsn.googlegroups.com>, Thu, 5 Jun 2008 15:09:23, Yin99
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top